package com.homework2; public class ClassA implements InterfaceA { @Override public int method(int n) { int sum = 0; for(int i = 0; i<=n;i++) { sum+=i; } return sum; } }
package com.homework2; public class ClassB implements InterfaceA { @Override public int method(int n) { int sum = 1; for(int i = 1; i<=n;i++) { sum*=i; } return sum; } }
package com.homework2; public interface InterfaceA { //定义抽象方法 int method(int n); }
package com.homework2; public class E { public static void main(String[] args) { ClassA a= new ClassA(); InterfaceA ia= a; System.out.println(a.method(10)); ClassB b= new ClassB(); InterfaceA ib= b; System.out.println(b.method(5)); } }
时间: 2024-10-24 02:20:13