python 在一個類的方法內部,直接讓其他類的對象調用其他類的方法

在一個類的方法內部,直接讓其他類的對象調用其他類的方法,如:

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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章