public class FuncRefTest {
public static void main(String[] args) {
Map<String, ICommand> cmds = new HashMap<>();
CommandA ca = new CommandA();
cmds.put("comm0", ca::serv0);
cmds.put("comm1", ca::serv1);
cmds.put("comm2", ca::serv2);
cmds.get("comm0").serv(0, "comm0");
cmds.get("comm1").serv(1, "comm1");
cmds.get("comm2").serv(2, "comm2");
}
}
interface ICommand {
void serv(int val, String txt);
}
// CommandA 并没有实现 ICommand 接口
class CommandA {
public void serv0(int val, String txt) {
System.out.println(String.format("serv0: %d %s", val, txt));
}
public void serv1(int val, String txt) {
System.out.println(String.format("serv1: %d %s", val, txt));
}
public void serv2(int val, String txt) {
System.out.println(String.format("serv2: %d %s", val, txt));
}
}
java 方法引用示例
最新推荐文章于 2023-02-13 23:27:57 发布