public interface Function<T, R>{
R apply(T t);
}
例如:
package com.example.demo.vo;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @author LLY
* @version 1.0
* @date 2022/9/28 15:43
*/
public final class FunctionTest {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 打印并返回名称
*/
public String printlnReturnName(String name){
System.out.println(name);
return name;
}
public void functionMethod(Function<String,String> function, String name){
function.apply(name);
}
public static void main(String[] args) {
FunctionTest functionTest = new FunctionTest();
functionTest.functionMethod(functionTest::printlnReturnName, "你的名字");
}
}
package com.example.demo.vo;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @author LLY
* @version 1.0
* @date 2022/9/28 15:43
*/
public final class FunctionTest {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/**
* 打印并返回名称
*/
public String printlnReturnName(){
System.out.println(this.getName());
return this.getName();
}
public void functionMethod(Function<FunctionTest,String> function){
function.apply(this);
}
public static void main(String[] args) {
FunctionTest functionTest = new FunctionTest();
functionTest.setName("你的名字");
functionTest.functionMethod(FunctionTest::printlnReturnName);
}
}
看懂没,只要 function.apply(this); apply(对象) 是指定的对象,就能使用 FunctionTest::printlnReturnName 这种方式写