# 继承 重用代码 提高软件的可扩展性 class Animal(): def __init__(self, name): print(1) self.name = name pass def eat(self, food): print(self.name + "吃" + food) pass def drink(self, food): print(self.name + "喝" + food) pass pass class Dog(Animal): def __init__(self, name): super().__init__(name) print("dog" + str(2)) pass def speak(self): print(self.name + '旺旺') pass pass class Cat(Animal): def __init__(self, name): super().__init__(name) print(3) pass def speak(self): print(self.name + "喵喵") pass pass # Monster 怪物 四不像 class Monster(Cat, Dog): # 多继承 Python3的C3算法 多重继承 def __init__(self, name): # super(Dog, self).__init__(name) super().__init__(name) # 这个问题可能是python自己的bug pass def speak(self): super().speak() pass pass dog = Dog('二哈') dog.eat('大棒骨') cat = Cat('Mikey') cat.eat('小老鼠') monster = Monster('四不像') monster.eat('吃人') monster.speak() print(Monster.mro())
Python基础:Python的继承,多继承和多重继承举例
最新推荐文章于 2023-08-08 19:38:59 发布