Author:zhuyahao Time:2026/4/17 Desc:题目 2继承特性 定义一个 Animal 类有一个 speak 方法打印 I am an animal。 再定义一个 Cat 类继承自 Animal 类并重写 speak 方法打印 Meow 创建 Cat 类的对象并调用 speak 方法。 class Animal: 动物基类 def speak(self): print(I am an animal) class Cat(Animal): 猫类继承自动物类 def speak(self): print(Meow) if __name__ __main__: # 创建 Cat 对象 cat Cat() # 调用 speak 方法会调用 Cat 类重写后的方法 print(Cat 对象的 speak 方法) cat.speak() # 验证继承关系 print(\n验证继承关系) print(fcat 是 Cat 的实例{isinstance(cat, Cat)}) print(fcat 是 Animal 的实例{isinstance(cat, Animal)}) # 演示父类方法 print(\n如果直接创建 Animal 对象) animal Animal() animal.speak()