# 导入随机数包
import random
# 第一个类 动物类
class Animal:
# 定义属性
def __init__(self,animal,weight,call,food):
self._animal=animal
self._weight=weight
self._call=call
self._food=food
def call(self):
self._weight -=5
print(f"{self._animal}叫了一声{self._call},当前体重{self._weight}")
def eat(self,food):
if self._food==food:
self._weight +=10
else:
self._weight -=10
print(f"{self._animal}吃了{food},当前体重{self._weight}")
def getWeight(self):
print(f"{self._animal},当前体重{self._weight}")
return self._weight
# 第二个类 老虎类
class Tiger(Animal):
# 定义属性
def __init__(self):
super().__init__("老虎",200,"wow","肉")
# 第三个类 羊类
class Sheep(Animal):
def __init__(self):
super().__init__("羊",100,"mie","草")
# 第四个类 房间类
class Room:
# 属性
def __init__(self):
# 定义一个变量 动物列表
self._animals=[]
def putAnimal(self,animal):
self._animals.append(animal)
def getAnimal(self):
return self._animals
# 第五个类 饲养员类
class Keep:
# 定义属性
def __init__(self):
# 定义一个变量 存房间列表
self._rooms=[]
def putAnimalInRoom(self):
# 10个房间里都要放动物
for _ in range(10):
num = random.randint(1, 2)
if num==1:
animal=Tiger()
else :
animal=Sheep()
# 实例化房间 为了调用房间的放动物方法
room=Room()
room.putAnimal(animal)
# 将房间放到房间列表里
self._rooms.append(room)
def keep(self):
count=0
# 喂动物 10个房间都需要
for room in self._rooms:
count+=1
print(f"第{count}个房间")
animals=room.getAnimal()# 从房间里获得动物列表
for animal in animals:
animal.getWeight()
animal.call()
food=animal._food
animal.eat(food)
animal.getWeight
# 调用饲养员
k=Keep()
k.putAnimalInRoom()
k.keep()