Write 2 interfaces:interfacea and interfaceb; in interface interfacea There is a method void in the
Printcapitalletter (); there is a method in interface interfaceb void printlowercaseletter () ; then
write a class after Print Implementation interface Interfacea and interfaceb, requires printcapitalletter () Method
implements the output capital English alphabet function,the Printlowercaseletter () method implements the output lowercase English
the function of the alphabet. Write a main class E, create the Print object in the main method of the primary class e , and assign
value to Interfacea variable a, Object a calls the printCapitalletter method, and finally in the main class E
the main method of creating the Print object and assigning a value to the interfaceb variable b , the object b called
Printlowercaseletter method.
Package Ersan; Public Interface interfacea { void printcapitalletter (); }
Package Ersan; Public Interface interfaceb { void printlowercaseletter ();}
Print
Package Ersan; Public classPrint implements Interfacea, interfaceb{@Override Public voidPrintcapitalletter () {//TODO Auto-generated method stubsSystem. out. println ("Abdef"); } @Override Public voidPrintlowercaseletter () {//TODO Auto-generated method stubsSystem. out. println ("ABCDEF"); }}
package Ersan; public class E { public static void Main (string[] args) { // TODO Auto-generated method stub Interfacea a =new Print (); A.printcapitalletter (); Interfaceb b =new Print (); B.printlowercaseletter (); }
(1) write an interface:interfacea, which contains only one method, int (int n);
Package Ersan; Public Interface interfacea { int method (int n); }
(2) write a class:ClassA to implement the interface interfacea, implementing the int method (int n) Interface Side
the calculation of 1 to n is required ;
package Ersan; public class ClassA implements Interfacea {@Override public< /span> int method (int N) { // TODO auto-generated method stub int b=0 ; for (int i= 1 ; I<=n;i++) {b =i+b; return b; }}
(3) write another class:ClassB to Implement Interface Interfacea, Implementing int method (int n) Interface
method is required to calculate the factorial of n (n! );
package Ersan; public class ClassB implements Interfacea {@Override public< /span> int method (int N) { // TOD auto-generated method stub
int
b=1 ; for (int i= 1 ; I<=n;i++) {b =b*i; return b; }}
(4) write test class e, test the implementation using the form of an interface callback in the main method oftest class e
The class of the interface.
Package Ersan; Public classE { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsInterfacea a=NewClassA (); System. out. println (A.method (Ten)); Interfacea b=NewClassB (); System. out. println (B.method (5)); }}
Java Basics-Interface