BankTemplateMethod抽象类
public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println("取号排队"); } public abstract void transact(); //办理具体的业务 //钩子方法 public void evaluate(){ System.out.println("反馈评分"); } public final void process(){ //模板方法!!! this.takeNumber(); this.transact(); this.evaluate(); } }
测试 类client
public class Client { public static void main(String[] args) { BankTemplateMethod btm = new DrawMoney(); btm.process(); //采用匿名内部类 BankTemplateMethod btm2 = new BankTemplateMethod() { @Override public void transact() { System.out.println("我要存钱!"); } }; btm2.process(); BankTemplateMethod btm3 = new BankTemplateMethod() { @Override public void transact() { System.out.println("我要理财!我这里有2000万韩币"); } }; btm3.process(); } } class DrawMoney extends BankTemplateMethod { @Override public void transact() { System.out.println("我要取款!!!"); } }
时间: 2024-11-05 18:54:18