public void queueUp() { System.out.println(this.name + "排队取号等候"); }
public void service(String type) { System.out.println(this.name + "办理" + type + "业务"); }
public void evaluate() { System.out.println(this.name + "反馈评分"); } }
客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class Client {
public static void main(String[] args) { People p1 = new People("张三"); p1.queueUp(); p1.service("存钱"); p1.evaluate(); System.out.println("=============="); People p2 = new People("李四"); p2.queueUp(); p2.service("取钱"); p2.evaluate(); } }
其中,work 方法就是一个模板方法,该方法不管方法实现,只负责调用方法顺序。子类继承 service 方法实现不同的业务需求。
客户端:
1 2 3 4 5 6 7 8 9 10 11 12
public class Client {
public static void main(String[] args) { People p1 = new ZhangSan("张三"); p1.work(); System.out.println("==========="); People p2 = new LiSi("李四"); p2.work(); } }