组合

1.给一个人类添加武器的组合
class Weapon:
def prick(self,obj): # 这是该装备的主动技能,扎死对方
obj -= 500 # 假设攻击力是500
print(obj)


class Person: # 定义一个人类
role = 'person' # 人的角色属性都是人

def __init__(self, name):
self.name = name # 每一个角色都有自己的昵称;
self.weapon = Weapon() # 给角色绑定一个武器;


egg = Person('egon')
obj = 1000
egg.weapon.prick(obj)

2.人类和医院类的组合
class Hospital():
"医院类"
def __init__(self,name,addr,type):
self.name = name
self.addr = addr
self.type = type

def accpatient(self):
print("%s开始接受患者"%self.name)

def regpatien(self,price):
# print("治疗费用为%s"%(price))
price = price + 120.50-150
return price

class Patient():
"患者类"
def __init__(self,patientname,age,sex,hospital):
self.patientname = patientname
self.age = age
self.sex =sex
self.hospital = hospital

def tohispital(self):
print("%s去%s检查,检查为:%s"%(self.patientname,self.hospital.name,self.hospital.regpatien(330)))

#实例化医院
hospital = Hospital("无锡市人民医院","江苏省无锡市人民大道","三甲")
#实例化患者
patient1 = Patient("李明",24,"男",hospital)
#调用患者函数方法
patient1.tohispital()


class BirthDate:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day

3.老师类和生日类还有课程类的组合
class Couse:
def __init__(self,name,price,period):
self.name=name
self.price=price
self.period=period

class Teacher:
def __init__(self,name,gender,birth,course):
self.name=name
self.gender=gender
self.birth=birth
self.course=course
def teach(self):
print('teaching')

p1 =Teacher('egon','male',BirthDate('1995','1','27'),Couse('python','28000','4 months'))
print(p1.birth.year,p1.birth.month,p1.birth.day)
print(p1.course.name,p1.course.price,p1.course.period)









原文地址:https://www.cnblogs.com/jmc218/p/11836648.html