Combination of Python_ Classes

Source: Internet
Author: User

There is no common denominator between Class A and Class B, but there is a correlation between Class A and Class B, for example, the hospital class and the patient class are two completely different classes, there is no association between them, but the patient belongs to the hospital. At this point we are going to use a combination of classes to correlate hospital and patient classes. For details, see:

This part of the code is:

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

Combination of Python_ Classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.