publicclassTankextendsElement { @Override public String getEquipment() { return"我是一辆坦克"; } }
publicabstractclassBaseEquipmentextendsElement{ }
publicclassAttackEquipmentextendsBaseEquipment { private Element element; publicAttackEquipment(Element element){ this.element=element; } @Override public String getEquipment() { return element.getEquipment()+",增加攻击力"; } }
publicclassSpeedEquipmentextendsBaseEquipment{ private Element element; publicSpeedEquipment(Element element){ this.element=element; } @Override public String getEquipment() { return element.getEquipment()+",增加速度"; } }
publicclassLifeEquipmentextendsBaseEquipment{ private Element element; publicLifeEquipment(Element element){ this.element=element; } @Override public String getEquipment() { return element.getEquipment()+",增加生命"; } }
在最终的调用的时候,我们需要什么装备,就可以直接使用。调用如下:
1 2 3 4 5 6
Elementtank=newTank(); tank = newLifeEquipment(tank); tank = newAttackEquipment(tank); tank = newSpeedEquipment(tank); System.out.println(tank.getEquipment());