![(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测试 (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测试](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
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); } }