(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测试

package a;

public interface InterfaceA {
    int method(int n);

}
package a;

public class ClassA implements InterfaceA {

    public int method(int n) {
        int sum=0;
        for (int i = 1; i <=n; i++) 
        {            
            sum+=i;            
        }
        System.out.println("1+2+3+......+"+n+"="+sum);
        return sum;
    }

}
package a;

public class ClassB implements InterfaceA {

    public int method(int n) {
        int sum=1;
        for (int i = 1; i <=n; i++) 
        {            
            sum*=i;            
        }
        System.out.println(n+"!="+sum);
        return sum;
    }

}
package a;

public class E {

    public static void main(String[] args) {
        InterfaceA a=new ClassA();
        InterfaceA b=new ClassB();
        a.method(5);
        b.method(5);

    }

}

posted @ 2016-05-24 14:48  凌零聆  阅读(4709)  评论(0编辑  收藏  举报