Static methods inside the interface
interface DemoStatic { static void outputInfo () { System.out.println("this is demo 'static' "); } }
Defaule method inside the interface
interface DemoDefault{ default void fun() { System.out.println("this is demo 'default'"); } }
Use
class DemoClass implements DemoDefault {
}
public class DemoInterface {
public static void main(String[] args) {
//1 static
DemoStatic.outputInfo();
//2 default
DemoDefault demoClass = new DemoClass();
demoClass.fun();
}
}
Execution result
this is demo 'static'
this is demo 'default'
lambda
*interface
interface IDemoLambda {
void print();
}
Callback function
private static void func1(IDemoLambda1 iDemoLambda) { iDemoLambda.print(); }
Callback function
public class DemoLambda {
public static void main(String[] args) {
// Realization of inner class func(new IDemoLambda() {
@Override
public void print() {
System.out.println("before java 1.8 innerclass");
}
});
// Realization of Lambda func1(() -> { System.out.println("after java 1.8 lambda"); }); } }
lambda (a, b)-> a + b // formula
interface IDemoLambda3 { int add (int a , int b); }
func3((a,b)->a+b);
private static void func3(IDemoLambda3 iDemoLambda3) { System.out.println(iDemoLambda3.add(20, 30)); }
Recommended Posts