【无标题】

按要求编写Java程序:

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

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

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

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

 

Interface_A接口

public interface Interface_A {
    int method(int n);
}

Class_A类

public class Class_A implements Interface_A{

        public int method(int n)
        {
            int sum = 0;
            for(int i = 1; i<=n;i++)
            {
                sum+=i;
            }
            return sum;

        }
    }

Class_B类

public class Class_B {
    public int method(int n)
    {
        int sum = 1;
        for(int i = 1; i<=n;i++)
        {
            sum*=i;
        }
        return sum;
    }

}

E类

public class E {
    public static void main(String[] args) {
        Interface_A sc;
        sc =new Class_A();
        sc.method(4);
        System.out.println(sc.method(4));
        Class_B b= new Class_B();
        System.out.println(b.method(4));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值