使用继承的语法写出如下代码 实现主人与宠物玩耍功能 和狗狗玩接飞盘游戏,狗狗的健康值减少10,与主人亲密度增加5 和企鹅玩游泳游戏,企鹅的健康值减少10,与主人亲密度增加5 给Dog添加接飞盘方法catchingFlyDisc( ) 给Penguin添加游泳方法swimming( ) 给主人添加play(Pet pet)方法
时间: 2023-06-09 14:03:02 浏览: 110
class Person:
def __init__(self, name):
self.name = name
def play(self, pet):
print(f"{self.name} is playing with {pet.__class__.__name__}.")
if isinstance(pet, Dog):
pet.catchingFlyDisc()
elif isinstance(pet, Penguin):
pet.swimming()
class Pet:
def __init__(self, health_value=100, intimacy=0):
self.health_value = health_value
self.intimacy = intimacy
def play_with_master(self):
self.health_value -= 10
self.intimacy += 5
class Dog(Pet):
def catchingFlyDisc(self):
print("The dog is catching the fly disc.")
self.play_with_master()
class Penguin(Pet):
def swimming(self):
print("The penguin is swimming.")
self.play_with_master()
person = Person('Tom')
dog = Dog()
penguin = Penguin()
person.play(dog)
person.play(penguin)
阅读全文