Python implementation Pet Hospital basic functions
Object-oriented realization pet hospital pet behavior
Development process is as follows:
Object-Oriented Analysis: OOA: to analyze the behavior of objects and object-based
side type Object Design: OOD: The results of the analysis of the code is reduced to
object-oriented programming: of OOP: demand implemented through code
object-oriented test: the OOT: Run test function process
OOA: to analyze the behavior of objects and object-oriented
OOA: Demand in the presence of an object
pet
hospital
OOD: The result of the analysis is reduced to the code
OOD: Object required properties and behavior of
pet nickname, behavioral health rehabilitation
hospital Hospital name, behavior therapy
OOP: implementation requirements through code
class Pet:
""" 宠物类型"""
def __init__(self,name,health):
"""声明初始值"""
#
self.name = name
self.health = health
def recovery(self):
"""康复行为"""
#每次康复生命值+5
self.health += 5
print(self.name+'正在加血。。。。')
print('当前生命值为:',self.health)
class PetHospital:
""" 宠物医院类型"""
def __init__(self,name):
"""初始化属性"""
self.name = name
def Treat(self,pet):
"""医院治疗行为"""
while pet.health <=65:
print(pet.name,'开始治疗。。。。')
#调用宠物恢复功能
pet.recovery()
else:
print('宠物已经恢复健康了!!!')
#测试1
#创建对象
cat = Pet("小花猫",50)
mingming = PetHospital('明明医院')
#
mingming.Treat(cat)
#测试2
dog = Pet('大黄',70)
hp = PetHospital('超大')
hp.Treat(dog)