1 package Jiekou; 2 Public interface InterfaceA1 { Span style= "color: #008080;" >4 /* (1) write an interface: Interfacea, containing only one method int Method (int n); 5 (4) write Test Class E, In the main method of test class E, use the form of an interface callback to test the implementation of 6 interface class. */ 7 method (int 8 }
1 PackageJiekou;2 3 Public classClassAImplementsInterfaceA1 {4 /*5 * (2) Write a class: ClassA to Implement Interface Interfacea,6 * When implementing an int method (int n) interface approach, a calculation of 1 to n is required;7 */8 @Override9 Public intMethodintN) {Ten intSum=0; One for(inti=0;i<=n;i++) A { -sum+=i; - } the returnsum; - } - -}
1 PackageJiekou;2 3 Public classClassBImplementsInterfaceA1 {4 /*(3) write another class: ClassB to Implement Interface Interfacea,5 * When implementing the int method (int n) interface approach, it is required to calculate the factorial of n (n!);6 */7 @Override8 Public intMethodintN) {9 Ten intS=1; One for(inti=1;i<=n;i++) A { -s=s*i; - the } - returns; - } - +}
1 PackageJiekou;2 3 Public classE1 {4 5 Public Static voidMain (string[] args) {6 //TODO Auto-generated method stubs7 8ClassA a=NewClassA ();9System.out.println ("1-5 of the And for:" +a.method (5));TenClassB b=NewClassB (); OneSystem.out.println (The factorial of "5" is: "+b.method (5)); A } - -}
Java Fundamentals-Interface 2