程序在运行的过程中,根据传递的参数的不同,执行不同的函数或者操作不同的代码,这种在运行过程中才确定调用的方式称为运行时多态。
# 定义一个人的类型
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
# 康复的方法
def recure(self):
print("【%s】恢复健康,当前健康值为:%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康复了,健康值为:%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恢复健康了,健康值为:%s" % (self.name,self.helth))
# 定义医院
class Hospital(object):
def __init__(self):
self.name = "人民医院"
def care(self,person):
# 类型判断,判断变量是否为Person类型
if isinstance(person,Person):
if person.helth > 0 and person.helth <= 50:
print("手术中。。。。。")
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)