在一個類的方法內部,直接讓其他類的對象調用其他類的方法,如:
class Dog(object):
def __init__(self, name):
self.name = name
def game(self):
print("%s 普通玩" % self.name)
class XiaoTianQuan(Dog):
def game(self):
print("%s play in the sky" % self.name)
class Person(object):
def __init__(self, name):
self.name = name
def game_with_dog(self, dog):
print("%s with %s 玩" %(self.name, dog.name))
dog.game() #在Person類內部,直接讓狗對象調用狗的game方法
#wc = Dog("wc")
wc = XiaoTianQuan("fly wc")
xiaom = Person("xiaom")
xiaom.game_with_dog(wc)