???????程序在運行的過程中葬毫,根據(jù)傳遞的參數(shù)的不同枫夺,執(zhí)行不同的函數(shù)或者操作不同的代碼昭殉,這種在運行過程中才確定調(diào)用的方式稱為運行時多態(tài)忙干。
# 定義一個人的類型
class Person(object):
?????????? # name 姓名 age 年齡 helth健康值 【0~50極度虛弱,51~70亞健康培遵,86~100強壯】
?????????? def __init__(self, name, age, helth):
?????????????????????? self.name = name
?????????????????????? self.age = age
?????????????????????? self.helth = helth
?????????? # 康復(fù)的方法
?????????? def recure(self):
??????????????????????? print("【%s】恢復(fù)健康浙芙,當前健康值為:%s" % (self.name, self.helth))
class Man(Person):
????????????def __init__(self, name, age, helth):
???????????????????? Person.__init__(self, name, age, helth)
??????????? def recure(self):
??????????????????? print("%s:nnnnnnn康復(fù)了,健康值為:%s" % (self.name, self.helth))
class Women(Person):
?????? ??? def __init__(self, name, age, helth):
????????????? ?????? Person.__init__(self, name, age, helth)
????????? def recure(self):
???????????????????? print("%svvvvvvvv%s" % (self.name, self.helth))
class Animal(object):
?????????? def __init__(self,name,age,helth):
???????????????????? self.name = name
???????????????????? self.age = age
???????????????????? self.helth = helth
?????????? def recure(self):
??????????????????????print("[%s]dddd恢復(fù)健康了,健康值為:%s" % (self.name,self.helth))
# 定義醫(yī)院
class Hospital(object):
??????????? def __init__(self):
???????????????????? self.name = "人民醫(yī)院"
???????????def care(self,person):
?????????? # 類型判斷籽腕,判斷變量是否為Person類型
?????????????????? ??if isinstance(person,Person):
????????????????????????????? if person.helth > 0 and person.helth <= 50:
??????????????????????????????????????????? print("手術(shù)中嗡呼。。皇耗。南窗。。")
??????????????????????????????????????????? person.helth += 30
??????????????????????????????????????????? person.recure()
????????????????????????????? elif person.helth > 50 and person.helth <= 70:
???????????????????????????????????????????? print("輸液中郎楼。万伤。。呜袁。敌买。。阶界。")
?????????????????????????????????????????????person.helth += 15
???????????????????????????????????????????? person.recure()
??????????????????????????? else:
???????????????????????????????????????????? print("健康")
????????????????? else:
???????????????????????????? print("請去動物門診")
hospital = Hospital()
man = Man("老王",22,30)
women = Women("小劉",33,60)
man1 = Man("大王",66,90)
hospital.care(man)
hospital.care(women)
hospital.care(man1)
a = Animal("兔子",33,66)
hospital.care(a)