class IStickWeapon(object):
def weapon_type(self):
raise NotImplementedError
def use_stick_Weapon(self):
print(f'Use:\t{self.weapon_type()}')
class Sword(IStickWeapon):
def weapon_type(self):
return "Sword"
class IChopWeapon(object):
def weapon_type(self):
raise NotImplementedError
def use_chop_weapon(self):
print(f'Use:\t{self.weapon_type()}')
class Knigfe(IChopWeapon):
def weapon_type(self):
return "knigfe"
class AdapterChopWeaponToStickWeapon(IStickWeapon):
def __init__(self,ChopWeapon):
self.__ChopWeapon = ChopWeapon
def use_stick_Weapon(self):
return self.__ChopWeapon.use_chop_weapon()
class Character(object):
def fight(self, weapon):
raise NotImplementedError
class King(Character):
def fight(self, weapon):
if isinstance(weapon, type(IStickWeapon())):
weapon.use_stick_Weapon()
else:
raise TypeError("This weapon is not StickWeapon")
def main():
Peter = King()
sword = Sword()
knigfe = Knigfe()
stick_knigfe = AdapterChopWeaponToStickWeapon(knigfe)
Peter.fight(sword)
Peter.fight(stick_knigfe)
if __name__ == "__main__":
main()