# 羊 老虎 饲养员
# 父类
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_room(self):
print(f'在第{self._room_num}个房间')
# 方法 获得体重
def get_w(self):
print('---------')
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.rooms = {}
# 方法
def put_animal(self):
# 10个动物放到10个房间 房间号 1到10
for room_num in range(1,3):
# 房间里可能是羊或老虎
animal = random.choice([Tiger,Sheep])
# 房间和动物对象 放入字典
self.rooms[room_num] = animal(room_num)
# 喂养
def keep(self):
for room_num,animal in self.rooms.items():
animal.get_room()
animal.get_w()
animal.call()
animal.eat(animal._food)
animal.get_w()
# 实例化饲养员对象
k = Keeper()
k.put_animal()
k.keep()