ITPub博客

首页 > IT职业 > IT生活 > Python_类的组合

Python_类的组合

IT生活 作者:deeply 时间:2021-09-09 09:33:59 0 删除 编辑

A类与B类之间没有共同点,但是A类与B类之间有关联,比如说,医院类与患者类是两个完全不同的类,他们之间没有任何关联,但是患者是属于医院的。此时我们就要用到类的组合来关联医院类与患者类。
该部分代码为:

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  priceclass 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()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1978/viewspace-2804746/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-05-30

  • 博文量
    215
  • 访问量
    24530