如何使用设计模式在抽象类中定义抽象方法,而某些方法方法可能会覆盖或更改其在子类中的行为?在这个例子中,public abstract class GameCharacter
有attack
一个应该是模式的方法(定义一些方法,GameCharacter
一些留空,在子类中被*覆盖*)。
public abstract class GameCharacter{
public void attack(GameCharacter opponent){
while(opponent.hitPoints > 0 && this.hitPoints > 0){
// some abstract method which behavior can be *redefined*
//if specific class is *overrides*
// some of this functions
// it should be pattern design
public void doDamageToOpponent{
doAttackOne(){ .... }; // cannot change
doAttackTwo(); // can change, be overridden in child class
}
public void doDamageToThis{ // counterattack
doAttackOne(){ .... }; // cannot change
doAttackTwo(); // can change, be overriden in child class
}
}