Java—接口

1.说出下面程序的执行结果:

interface InterfaceA { 
    String S = "good "; 
    void f(); 
} 

abstract class ClassA { 
    abstract void g(); 
} 

class ClassB extends ClassA implements InterfaceA { 
    void g() { 
    System.out.print(S); 
    } 

    public void f() { 
    System.out.print(" "+ S); 
    } 
} 
public class Test { 
    public static void main(String[] args) { 
        ClassA a = new ClassB(); 
        InterfaceA b = new ClassB(); 
        a.g(); 
        b.f(); 
    } 
} 

执行结果为:good good

2.编程题:
利用接口做参数,写个计算器,能完成加减乘除运算。
(1)定义一个接口Compute含有一个方法int computer(int n, int m)。
(2)设计四个类分别实现此接口,完成加减乘除运算。
(3)设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。
(4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。

interface ICompute  {
    int computer(int n, int m);
}

//加法
class Add implements ICompute {
    public int computer(int n, int m) {
        return n + m;
    }
}

//减法
class Sub implements ICompute {
    public int computer(int n, int m) {
        return n - m;
    }
}

//乘法
class Mul implements ICompute {
    public int computer(int n, int m) {
        return n * m;
    }
}

//除法
class Div implements ICompute {
    public int computer(int n, int  m) {
        return n / m;
    }
}

class UseCompute {
    public void useCom(ICompute com, int one, int two) {
        int ret = com.computer(one, two);
        System.out.println("计算结果: "+ret);
    }
}

public class Test { 
    public static void main(String[] args) { 
        UseCompute uc = new UseCompute();
        uc.useCom(new Add(), 5, 7);
        uc.useCom(new Sub(), 12, 7);
        uc.useCom(new Mul(), 5, 4);
        uc.useCom(new Div(), 14, 7);
    } 
} 

3.按如下要求编写Java程序:
(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
(2)定义接口B,里面包含抽象方法void setColor(String c)。
(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
(5)创建主类来测试类Cylinder。

interface IA {
    float PI = 3.14f;
    public abstract double area();
}

interface IB {
    public abstract void setColor(String c);
}

interface IC extends IA, IB {
    public abstract void volume();
}

class Cylinder implements IC {
    private int radius;
    private int height;
    private String color;

    public Cylinder(){}  

    public Cylinder(int radius, int height, String color){  
        this.radius = radius;  
        this.height = height;  
        this.color = color;  
    }  

    public int getRadius() {  
        return radius;  
    }  

    public void setRadius(int radius) {  
        this.radius = radius;  
    }  

    public int getHeight() {  
        return height;  
    }  

    public void setHeight(int height) {  
        this.height = height;  
    }  

    public String getColor() {  
        return color;  
    }  

    public void setColor(String c) {  
        this.color = color;       
    }  

    //Override  
    public double area() {  
        return 2*radius*PI*height + 2*radius*radius*PI;  
    }  

    //Override  
    public void volume() {  
        float volumeCount = radius*radius*PI*height;  
        System.out.println("体积为: " + volumeCount);  
    }  
}

public class Test {
    public static void main(String[] args) {
        Cylinder c = new Cylinder(4, 2, "green");
        System.out.println("表面积为: "+c.area());
        c.volume();
    }
}

4.完数: 一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程 找出1000以内的所有完数。

public class Test {
    public static void isWanShu() {
        for(int i = 1; i <= 1000; i++) {
            int sum = 0;
            for (int j = 1; j < i; j++) {
                if(i % j == 0){
                    sum += j;
                }
            }
            if(sum == i) {
                System.out.print(i+" ");
            }   
        }

    }
    public static void main(String[] args) {
        isWanShu();
    }
}

猜你喜欢

转载自blog.csdn.net/yubujian_l/article/details/79857902