- implementation of the interface : use interface definition: The form is as follows
1 Interface printable{2 Final int MAX = +; 3 void Add (); 4 float sum (floatfloat y); 5 }
Interface interface Name {interface Body}, in which the interface body is only abstract methods and constants of the declaration, constants are public static type, methods are public type, the above interface printable can be expressed in the following form:
1 interface printable{ 2 public final static int max= 100 3 public Abstract void add (); 4 public Abstract float sum (float X,float y); 5 }
To implement an interface with a class, the sample code is as follows:
1 Public Interfacecomputable{//Declaration Interface Computable2 intMAX = 46;3 intFintx);4 }5 6 Public classChinaImplementscomputable{//The China class implements the interface7 intNumber ;8 Public intFintx) {//overriding the interface F method public note9 intsum = 0;Ten One for(inti = 1; I <= x; i++){ Asum = sum +i; - } - the returnsum; - } - } - + Public classCalmaxImplementscomputable{ - intNumber ; + Public intFintx) { A returnMAX +x; at } -}
Java interface, Lambda learning