老虎 羊 喂养

发布时间 2023-07-23 21:31:05作者: 胖豆芽
# 羊  老虎
import random
# 父类
class Animal():
    # 属性
    def __init__(self,animal,w,call,food,room_num):
        self._animal = animal
        self._w = w
        self._call = call
        self._food = food
        self._room_num = room_num
    # 获取体重
    def get_w(self):
        print(f"{self._animal}当前的体重是{self._w}")
    #
    def eat(self,food):
        print(f"{self._animal},吃了{food}当前的体重是{self._w}")
    #
    def call(self):
        print(f"{self._animal},叫了一声{self._call}当前的体重是{self._w}")
# 子类  老虎
class Tiger(Animal):
    # 属性
    def __init__(self,room_num):
        # 赋值
        super().__init__("tiger",200,'wow','meat',room_num)
# 子类  羊
class Sheep(Animal):
    # 属性
    def __init__(self,room_num):
        # 赋值
        super().__init__("sheep",100,'mie','grass',room_num)
# 饲养员
class Keeper():
    # 属性
    def __init__(self):
        # 变量
        self.dict_room= {}#{房间号:动物类}
    # 方法 放动物到房间
    def put_animal(self):
        # 10个动物
        for room_num in range(1,11):

            # 动物类被定义  是 老虎或羊类 ,老虎或者羊类有 必传参数 房间号
            animal=random.choice([Tiger,Sheep])
            # 将老虎或羊类有 必传参数 房间号 放到字典中
            self.dict_room[room_num]=animal(room_num)
        print(self.dict_room)

    # 方法   喂养动物
    def keep(self):
        for room_num,animal in self.dict_room.items():

            # 实例化 一个动物类
            a = animal

            # 调用实例化后的a的函数
            a.get_w()
            a.call()
            a.eat(a._food)
            a.get_w()

# 实例化饲养员
k = Keeper()
k.put_animal()
k.keep()