行为参数化,可以帮助复用代码:
public class TestMaxBy {
public static void main(String[] args) {
crossChannel(() -> System.out.println("wave wings once"));
crossChannel(() -> {System.out.println("wave wings once");
System.out.println("rest, then another time");});
}
static void crossChannel(Fly fly){
System.out.println("开始过海峡");
fly.waveWings();
System.out.println("已经飞过海峡");
}
private interface Fly{
void waveWings();
}
}
本文介绍了一种通过行为参数化实现代码复用的方法,展示了如何定义接口并在不同场景中使用同一方法来调用不同的行为。这种方法可以提高代码的灵活性和可维护性。

被折叠的 条评论
为什么被折叠?



