模板方法模式
模板方法就是在模板中按照一定的规则和顺序调用基本方法。
public abstract class Parent {
public abstract void talk();
public abstract void eat();
protected abstract boolean can_eat();
public void doSomething(){
talk();
if(can_eat()) eat();
}
}
public class Son extends Parent {
private boolean son_eat;
public Son( boolean son_eat){
this.son_eat=son_eat;
}
public void eat() {
System.out.println("-------son eat");
}
public void talk() {
System.out.println("-------son takl");
}
protected boolean can_eat() {
return son_eat;
}
}
--子类可以影响父类的行为。父类调用子类的方法
模板方法就是在模板中按照一定的规则和顺序调用基本方法。
public abstract class Parent {
public abstract void talk();
public abstract void eat();
protected abstract boolean can_eat();
public void doSomething(){
talk();
if(can_eat()) eat();
}
}
public class Son extends Parent {
private boolean son_eat;
public Son( boolean son_eat){
this.son_eat=son_eat;
}
public void eat() {
System.out.println("-------son eat");
}
public void talk() {
System.out.println("-------son takl");
}
protected boolean can_eat() {
return son_eat;
}
}
--子类可以影响父类的行为。父类调用子类的方法