简单的说,模板方法就是回调函数
public abstract class MyHandler {
private void m1(){
System.out.println("m1");
}
private void m2(){
System.out.println("m2");
}
/**
* 模板方法
*/
abstract void m3();
public void run(){
m1();
m2();
m3();
}
}
public class Test {
public static void main(String[] args) {
MyHandler h = new MyHandler() {
@Override
void m3() {
System.out.println("m3");
}
};
h.run();
}
}
运行结果:
m1
m2
m3
来源:oschina
链接:https://my.oschina.net/icefoxhz/blog/4956919