有以下类定义:动物类属性:名称、年龄、性别 行为:吃 猫类继承动物行为: 吃重写 捉老鼠 狗类继承动物行为: 吃重写 看家
代码实现:
class Animal: def init(self, name, age, gender): self.name = name self.age = age self.gender = gender
def eat(self):
print("Animal is eating")
class Cat(Animal): def eat(self): print("Cat is eating")
def catch_mouse(self):
print("Cat is catching mouse")
class Dog(Animal): def eat(self): print("Dog is eating")
def watch_house(self):
print("Dog is watching house")
原文地址: https://www.cveoy.top/t/topic/fEvY 著作权归作者所有。请勿转载和采集!