Project

General

Profile

Files » animal.py

UserName LastName, 01/15/2025 04:39 PM

 
"""创建一个基础类 Animal,实现通用的属性和方法。
创建派生类 Dog 和 Cat,继承自 Animal 并增加各自的独特行为。"""
class Animal :
def __init__(self,name,age):
self.name=name
self.age = age
def call(self):
print(f"{self.name}")

def info(self):
return f"{self.name} is {self.age} years old."


class Dog(Animal):
def call(self):
super().call()
print("汪汪叫")

class Cat(Animal):
def call(self):
super().call()
print("喵喵叫")
class Bird(Animal):
def call(self):
super().call()
print("啾啾叫")
dog=Dog("xiaogou",5)
cat=Cat("xiaomao",3)
bird=Bird("xiaoniao",5)
dog.call()
cat.call()
bird.call()

print(dog.info())
print(cat.info())
print(bird.info())
(78-78/437)