import random标签:__,weight,self,老虎,动物,animal,._,def From: https://www.cnblogs.com/haha1988/p/17563779.html
# 动物类
class Animal:
def __init__(self, animal_type, weight, cries, food):
self._animal_type = animal_type
self._weight = weight
self._cries = cries
self._food = food
def getWeight(self):
print(f"{self._animal_type}的体重是{self._weight}")
return self._weight
def eat(self, food):
if self._food == food:
self._weight += 10
else:
self._weight -= 10
print(f"{self._animal_type}吃了{food}后,当前体重为{self._weight}")
return self._weight
def cries(self):
self._weight -= 5
print(f"{self._animal_type}发出了{self._cries}的叫声,当前体重为{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 getAnimals(self):
return self._animals
# 饲养员类
class Zookeeper:
def __init__(self):
self._rooms = []
def putAnimalInRooms(self):
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 feedAnimals(self):
count=0
for room in self._rooms:
count += 1
print(f'第{count}个房间')
animals = room.getAnimals()
for animal in animals:
animal.getWeight()
animal.cries()
food = animal._food
animal.eat(food)
animal.getWeight()
zookeeper = Zookeeper()
zookeeper.putAnimalInRooms()
zookeeper.feedAnimals()