动物园相关类功能扩展实现求助
动物园相关类功能扩展实现求助
嘿,我看你已经搭好了动物园和各类动物的基础框架,现在卡在那三个功能的实现上了对吧?别着急,咱们一步步把每个要求都落地,顺便帮你修正基础代码里的小bug~
首先先修正基础类里的一个关键问题:原来的Feline类中,__init__里定义的是self.__characteristic(双下划线私有变量),但look方法里调用的是self._characteristic,这会导致子类访问不到这个变量而报错,所以要把Feline里的__characteristic改成单下划线的_characteristic,这样子类才能正确继承和覆盖。
接下来咱们逐个实现要求的功能:
1. 改写looking方法为静态方法,生成单字符串
我们需要创建一个静态方法,接收合并后的动物+鸟类列表,用map把每个对象的look()结果提取出来,再用reduce把所有字符串拼接成一个整体(中间加空行分隔,方便阅读)。注意要先导入functools.reduce哦。
2. 实现find_canine方法
这个方法需要用filter筛选出_animals列表中属于Canine类(包括其子类比如Wolf)的对象,然后把筛选结果传给刚才的静态方法,返回格式化后的字符串。
3. 实现find_tiger方法
按照要求用re.search来匹配老虎,我们可以检查每个动物的类名(obj.__class__.__name__)是否包含"Tiger",结合filter和re.search来筛选,再用map提取look()结果,最后拼接成字符串。
完整修改后的代码
import functools import re class Animal(): def __init__(self): self.__number_of_hands = 0 self.__number_of_legs = 4 def look(self): return "Number of hands: {hands}, Number of legs: {legs}".format(hands=self.__number_of_hands, legs=self.__number_of_legs) class Feline(Animal): def __init__(self): Animal.__init__(self) # 修正:把双下划线改成单下划线,子类才能访问 self._characteristic = "Feline belong to the cat family" def look(self): return super().look() + "\n" + self._characteristic class Tiger(Feline): def __init__(self): Feline.__init__(self) self._characteristic = "Tigers can roar and are lethal predators" def look(self): return super().look() + "\n" + self._characteristic class Wild_Cat(Feline): def __init__(self): Feline.__init__(self) self._characteristic = "Wild cats can climb trees" def look(self): return super().look() + "\n" + self._characteristic class Canine(Animal): def __init__(self): Animal.__init__(self) self._characteristic = "Canines belong to the dog family" def look(self): return super().look() + "\n" + self._characteristic class Wolf(Canine): def __init__(self): Canine.__init__(self) self._characteristic = "Wolves hunt in packs and have a leader" def look(self): return super().look() + "\n" + self._characteristic class Bird(): def __init__(self): self._number_of_legs = 2 self._number_of_wings = 2 def look(self): return "Number of legs: {legs}, Number of wings: {wings}".format(legs=self._number_of_legs, wings=self._number_of_wings) class Flight_Bird(Bird): def __init__(self): Bird.__init__(self) self._characteristic = "Flight birds fly and hunt for food" def look(self): return super().look() + "\n" + self._characteristic class Eagle(Flight_Bird): def __init__(self): Flight_Bird.__init__(self) self._characteristic = "Eagles fly extremely high and can see their prey from high up in the sky" def look(self): return super().look() + "\n" + self._characteristic # Part 2 修改后的Zoo类 class Zoo(): def __init__(self): self._animals = [] self._birds = [] def add(self, animal): if isinstance(animal, Bird): if len(self._birds) < 1: self._birds.append(animal) print("Added bird to the zoo.") else: print("Zoo full for birds.") elif isinstance(animal, Animal): if len(self._animals) < 2: self._animals.append(animal) print("Added animal to the zoo.") else: print("Zoo full for animals") else: print("Invalid input.Cannot add to the zoo.") # 1. 静态方法:生成所有动物鸟类的单字符串表示 @staticmethod def get_combined_look_string(items): # 用map获取每个item的look()结果 look_strings = map(lambda x: x.look(), items) # 用reduce拼接所有字符串,每个条目之间加空行分隔 return functools.reduce(lambda a, b: a + "\n\n" + b, look_strings) # 原来的looking方法可以调用静态方法来实现 def looking(self): all_items = self._animals + self._birds print(self.get_combined_look_string(all_items)) # 2. 实现find_canine方法 def find_canine(self): # 筛选出Canine类或其子类的对象 canines = list(filter(lambda x: isinstance(x, Canine), self._animals)) # 调用静态方法返回字符串 return self.get_combined_look_string(canines) # 3. 实现find_tiger方法 def find_tiger(self): # 用re.search匹配类名是否为Tiger tigers = list(filter(lambda x: re.search(r'Tiger', x.__class__.__name__), self._animals)) # 调用静态方法返回字符串 return self.get_combined_look_string(tigers) # 测试示例 if __name__ == "__main__": zoo = Zoo() zoo.add(Tiger()) zoo.add(Wolf()) zoo.add(Eagle()) print("=== 所有动物鸟类信息 ===") zoo.looking() print("\n=== 犬科动物信息 ===") print(zoo.find_canine()) print("\n=== 老虎信息 ===") print(zoo.find_tiger())
代码解释
- 静态方法
get_combined_look_string:接收一个包含动物/鸟类的列表,先用map把每个对象的look()结果转换成字符串列表,再用reduce把这些字符串用空行拼接成一个大字符串,满足“单个字符串表示”的要求。 find_canine方法:用filter结合isinstance判断每个动物是否属于Canine类(包括子类Wolf),然后把筛选后的列表传给静态方法生成结果。find_tiger方法:用re.search匹配对象的类名(x.__class__.__name__)是否包含"Tiger",筛选出老虎后同样传给静态方法生成字符串。
这样修改后,三个功能都能正常工作,同时也修正了基础类里的私有变量访问问题,应该能解决你之前遇到的报错啦~
备注:内容来源于stack exchange,提问作者Viktoriia Chornenka




