java基础 接口练习

1.编写2个接口:InterfaceAInterfaceB;在接口InterfaceA中有个方法voidprintCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然后写一个类Print实现接InterfaceA和InterfaceB,要求printCapitalLetter()方法实现输出大写英文字母表的功能,printLowercaseLetter()方法实现输出小写英文字母表的功能。再写一个主类E,在主类E的main方法中创建Print的对象并赋值给InterfaceA的变量a,对象a调用printCapitalLetter方法;最后再在主类E的main方法中创建Print的对象并赋值给InterfaceB的变量b,对象b调用printLowercaseLetter方法

//InterfaceA接口
public interface InterfaceA {
        void printCapitalLetter();
}
//InterfaceB接口
public interface InterfaceB {
         void printLowercaseLetter();
}
public class Print implements InterfaceA, InterfaceB {

    @Override
    public void printLowercaseLetter() {
    System.out.println("ABCDEFGHIJKLMNOPQISTUVWXYZ");

    }

    @Override
    public void printCapitalLetter() {
        System.out.println("abcdefghijklmnopqistuvwxyz");

    }

}
public class E {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //给Print创建对象,并调InterfaceA的接口
    Print a = new Print();
    a.printCapitalLetter();
    //给Print创建对象,并调InterfaceB的接口
    Print b =new Print();
    b.printLowercaseLetter();
    
    }

}

2.按要求编写Java程序:

1)编写一个接口:InterfaceA,只含有一个方法int method(int n)

2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算1到n的和;

3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口方法时,要求计算n的阶乘(n!);

4)编写测试类E,在测试类Emain方法中使用接口回调的形式来测试实现。

public interface InterfaceA2 {
    int method(int n);
}
//接口的实现
public class ClassA implements InterfaceA2 {

    
    int sum =0;
    public int method(int n) {
        // TODO 自动生成的方法存根
        for(int i =1;i<n;i++)
        {
            sum+=i;
        }
        System.out.println("1+2+3+4......+n="+sum);
        return sum;
    }

}
public class ClassB implements InterfaceA2 {

    
    int sum=0;
    public int method(int n) {
        // TODO 自动生成的方法存根
        for(int i =1;i<n;i++)
        {
            sum*=i;
        }
        System.out.println("1*2*3*4.....*n ="+sum);
        return sum;
    }

}
public class E2 {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //声明接口变量
        InterfaceA2 a;
        //实例化接口对象中存放对象的引用
        a = new ClassA();
        //接口回调
        a.method(6);
        //实例化接口对象中存放对象的引用
        a=new ClassB();
        //接口回调
        a.method(7);
    }

}

3.中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。

要求如下:

1)该应用程序中有一个“党中央”接口:CentralPartyCommittee,该接口中有个“坚持党的领导”方法:void partyLeader()

2)该应用程序中有一个“国务院”抽象类:StateCouncil,该抽象类中有个“安全生产”的抽象方法:abstract void safetyInProduction()

3)该应用程序中有一个“省政府”类:Province,该类继承StateCouncil抽象类并且实现CentralPartyCommittee接口;在实现partyLeader()方法时输出“我们各省人民一定坚持党的领导!”;在重写safetyInProduction()方法时输出“我们各省人民一定按照国务院的指示进行安全生产!”。

(4)该应用程序中有一个主类E,在主类E的main方法中创建Province类的对象,来测试Province类的功能。

public interface CentralPartyCommittee {
    void partyLeader();
}
//定义一个抽象类
public abstract class StateCouncil {
    
    abstract void safetyInProduction();
}
//继承抽象类并实现该接口
public class Province extends StateCouncil implements CentralPartyCommittee {

    @Override
    public void partyLeader() {
        System.out.println("我们各省人民一定坚持党的领导!");
    }

    @Override
    void safetyInProduction() {
        System.out.println(" 我们各省人民一定按照国务院的指示进行安全生产!");


    }

}
public class E3 {

    public static void main(String[] args) {
        Province a = new Province();
        a.partyLeader();
        a.safetyInProduction();

    }

}

4.看下图实现如下接口和类,并完成Adventure中的主方法。

//能飞的接口
public interface CanFly {
        void fly();
}
//游泳的接口
public interface CanSwim {
        void swim();
}
public abstract class ActionCharacter {
                       //抽象方法
            public abstract void finght(String emp);
                       //已经实现的方法
            public void speak(String s)
            {
                System.out.println(s);
            }
}
//继承并实现父类中所有的方法
public class Hero extends ActionCharacter implements CanSwim, CanFly {
        String name;
    public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
            
        }

    @Override
    public void fly() {
        System.out.println(name+"能飞");

    }

    @Override
    public void swim() {
        System.out.println(name+"能游");

    }

    @Override
    public void finght(String emp) {
        System.out.println(name+"还能打");

    }

}
public static void main(String[] args) {
        // TODO 自动生成的方法存根
            Hero hb = new Hero();
            hb.setName("大飞");
            hb.swim();
            hb.finght("是");
            hb.fly();
          //声明CanFly类型变量cf
            CanFly  cf;
           //将hb的值赋给cf
            cf =hb;
           //用cf调用fly方法
            cf.fly();
            CanSwim cs;
            cs=hb;
            cs.swim();
            ActionCharacter ac;
            ac=hb;
            ac.speak("说");
            ac.finght("打");
            
    }

}

5.利用接口做参数,写个计算器,能完成+-*/运算

1)定义一个接口Compute含有一个方法int computer(int n,int m);

2)设计四个类分别实现此接口,完成+-*/运算

3)设计一个类UseCompute,含有方法:

public void useCom(Compute com, int one, int two)

此方法要求能够:1.用传递过来的对象调用computer方法完成运算

                  2.输出运算的结果

4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算

//定义一个接口
public interface Compute {
    int computer(int n,int m);
}
//实现接口的类
public class Jian implements Compute {

    @Override
    public int computer(int n, int m) {
        int jian =n-m;
        System.out.println(n+"-"+m+"="+jian);
        return jian;
    }

}
public class Jia implements Compute {

    @Override
    public int computer(int n, int m) {
        // TODO 自动生成的方法存根
        int jia =n+m;
        System.out.println(n+"+"+m+"="+jia);
        return jia;
    }

}

public class Cheng implements Compute {


@Override
public int computer(int n, int m) {
// TODO 自动生成的方法存根
int cheng =n*m;
System.out.println(n+"*"+m+"="+cheng);
return cheng;
}


}

 
public class Chu implements Compute {

    @Override
    public int computer(int n, int m) {
        int chu =n/m;
        System.out.println(n+"/"+m+" ="+chu);
        return chu;
    }

}
public class UseComputer implements Compute {

    @Override
    public int computer(int n, int m) {
        // TODO 自动生成的方法存根
        return 0;
    }
      public void useCom(Compute com, int n, int m)
        {
            com.computer(n, m);
        }
}
public class TestUseComputer {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        UseComputer a = new UseComputer();
        Jia a1 =new Jia();
        Jian a2 = new Jian();
        Cheng a3 = new Cheng();
        Chu a4 = new Chu();
        a.useCom(a1, 4, 7);
        a.useCom(a2, 3, 5);
        a.useCom(a3, 2, 8);
        a.useCom(a4, 7, 1);
    }

}

原文地址:https://www.cnblogs.com/miracle-0807/p/5901644.html