python3.6
class Person:
def __init__(self, name):
self.name = name
def show(self):
print("装扮的", self.name)
class Finery(Person):
def __init__(self): #此处必须有,覆盖父类的__init__()
pass
def Decorate(self, comp):
self.comp = comp
def show(self): # 此例中该方法可以没有
if self.comp != None:
self.comp.show()
class Tshirts(Finery):
def show(self):
print("大T恤")
self.comp.show()
class BigTrouser(Finery):
def show(self):
print("垮裤")
self.comp.show()
class WearSneakers(Finery):
def show(self):
print("破球鞋")
self.comp.show()
class WearSuit(Finery):
def show(self):
print("西装")
self.comp.show()
class WearTie(Finery):
def show(self):
print("领带")
self.comp.show()
class WearLeatherShoes(Finery):
def show(self):
print("皮鞋")
self.comp.show()
if __name__ == "__main__":
per = Person("小菜")
print("第一种装扮:")
ts = Tshirts()
bt = BigTrouser()
sk = WearSneakers()
ts.Decorate(per)
bt.Decorate(ts)
sk.Decorate(bt)
sk.show()