行为参数化,可以帮助复用代码:
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();
}
}