public class Test {
protected interface FunctionEx {
void execute(String a);
default void defaultMethod() {
System.out.println("FunctionEx default executed!");
}
}
public String saySomething(String a) {
System.out.println("say :" + a);
return "";
}
public void doTest(String a, FunctionEx functionEx) {
System.out.print("doTest ");
functionEx.execute(a);
}
public void doTest2(String a) {
doTest(a, this::saySomething);
doTest(a, n -> this.saySomething(n));
doTest(a, n -> saySomething(n));
doTest(a, new FunctionEx() {
@Override
public void execute(String a) {
saySomething(a);
}
});
}
public static void main(String[] args) {
String str = "hello";
Test test = new Test();
test.doTest(str, n -> test.saySomething(n));
test.doTest(str, test::saySomething);
test.doTest(str, n -> {
test.saySomething(n);
});
test.doTest(str, new FunctionEx() {
@Override
public void execute(String a) {
test.saySomething(a);
defaultMethod();
}
});
System.out.println("doTest2 start");
test.doTest2(str);
}
}