weixin_44418968
2019-01-06 06:46
采纳率: 100%
浏览 1.2k

python子类继承父类时找不到__init__

UML

类PatientManagement有3个方法
1. add_patient 添加一个新病人(属于类 Patient)。病人对应一个ID,第一个ID为0,第二个为1,以此类推
2. get_patient 返回病人的ID
3.get_statistics 返回女病人和男病人的数量,以及平均BMI
病人和ID以字典的方式储存在变量 patients 中,以{id: Patient}形式,最后一个使用的ID储存在变量 last_used 中。
类 Patient 有两个方法
1. to_string 打印 名 姓 出生日 月 年 性别 身高(cm)体重(kg)
2. get_bmi 计算这个病人的BMI,BMI = weight in KG / (height in M)²

get_statistics调用get_bmi 且不再进行一变BMI计算

运行结果是这样

class Patient(object):

    def __init__(self, first_name, last_name, birth_year, birth_month, birth_day, sex, body_height, body_weight):
        self.first_name = first_name
        self.last_name = last_name
        self.year = birth_year
        self.month = birth_month
        self.day = birth_day
        self.sex = sex
        self.height = body_height
        self.weight = body_weight

    def to_string(self):
        print("Name: {} {}, geboren am {}.{}.{}, geschlecht:{}, {}cm, {}kg".format(self.first_name, self.last_name, self.day, self.month, self.year, self.sex, self.height, self.weight))

    def get_bmi(self):
        return self.weight / ((self.height/100) ** 2)


a =[]
b = []
patients = {}
i = 0
class PatientManagement(Patient):

    fpCount = 0
    mpCount = 0


    def add_patient(self):
        patients[i] = Patient()
        i += 1
        if self.sex == f:
            PatientManagement.fpCount += 1
        if self.sex == m:
            PatientManagement.mpCount += 1
        for k in patients.keys():
            a.append(k)
            b.append(patients[k])
        last_used = a[-1]

    def get_patient(self):
        new_dict = {v:k for k,v in patients.items()}
        patient_id = new_dict[Patient()]
        return patients_id

    def get_statistics(self):
        bmi = []
        for patient in b:
            bmi.append(patient.get_bmi())
        avg_bmi = np.mean(bmi)
        return "female Patients:{}, male Patients:{}, average BMI: {}".format(PatientManagement.fpCount, PatientManagement.mpCount, avg_bmi)

p1 = Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50)
pm1 = PatientManagement(p1)
pm1.add_patient()
pm1.get_patient()

这里报错是问题在哪里?


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-141-ebb011904724> in <module>()
      1 p1 = Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50)
----> 2 pm1 = PatientManagement(p1)
      3 pm1.add_patient()
      4 pm1.get_patient()

TypeError: __init__() missing 7 required positional arguments: 'last_name', 'birth_year', 'birth_month', 'birth_day', 'sex', 'body_height', and 'body_weight'
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_44418968 2019-01-08 01:16
    已采纳

    今天想出来了

    class Patient(object):
    
        def __init__(self, first_name, last_name, birth_year, birth_month, birth_day, sex, body_height, body_weight):
            self.first_name = first_name
            self.last_name = last_name
            self.year = birth_year
            self.month = birth_month
            self.day = birth_day
            self.sex = sex
            self.height = body_height
            self.weight = body_weight
    
        def to_string(self):
            print("Name: {} {}, geboren am {}.{}.{}, geschlecht:{}, {}cm, {}kg".format(self.first_name, self.last_name, self.day, self.month, self.year, self.sex, self.height, self.weight))
    
        def get_bmi(self):
            return self.weight / ((self.height/100) ** 2)
    
    
    
    
    class PatientManagement(Patient):
    
        fpCount = 0
        mpCount = 0
        a =[]
        b = []
        patients = {}
        i = 0
    
        def __init__(self):
            pass
    
        def add_patient(self,Patient):
            PatientManagement.patients[PatientManagement.i] = Patient
            PatientManagement.i += 1
            if Patient.sex == 'f':
                PatientManagement.fpCount += 1
            if Patient.sex == 'm':
                PatientManagement.mpCount += 1
            for k in PatientManagement.patients.keys():
                PatientManagement.a.append(k)
                PatientManagement.b.append(PatientManagement.patients[k])
            last_used = PatientManagement.a[-1]
    
        def get_patient(self,Patient):
            new_dict = {v:k for k,v in PatientManagement.patients.items()}
            patient_id = patients[i]
            return patient_id
    
        def get_statistics(self):
            bmi = []
            for patient in PatientManagement.b:
                bmi.append(patient.get_bmi())
            avg_bmi = np.mean(bmi)
            print("female Patients:{}, male Patients:{}, average BMI: {}".format(PatientManagement.fpCount, PatientManagement.mpCount, avg_bmi))
    
    patients = []
    patients.append(Patient("Meier", "Lena", 1988, 12, 12, 'f', 164, 50))
    patients.append(Patient("Müller", "Thomas", 1989, 11, 12, 'm', 180, 78))
    patients.append(Patient("Weber", "Meike", 1977, 4, 23, 'f', 164, 61))
    patients.append(Patient("Steiner", "War", 1988, 1, 13, 'm', 164, 48))
    patients.append(Patient("Lees", "Bay", 1998, 3, 16, 'f', 156, 70))
    patients.append(Patient("Rose", "Sophia", 1988, 8, 11, 'f', 164, 55))
    
    pm = PatientManagement()
    for patient in patients:
        print("Adding {} {}, bmi: {:.2f}".format(patient.first_name, patient.last_name,patient.get_bmi()))
        pm.add_patient(patient)
    
    pm.get_statistics()
    
    for i in range(len(patients)):
        print("{} {} {:.2f}".format(pm.get_patient(i).first_name, 
                                    pm.get_patient(i).last_name, 
                                    pm.get_patient(i).get_bmi()))
    

    输出结果

    Adding Meier Lena, bmi: 18.59
    Adding Müller Thomas, bmi: 24.07
    Adding Weber Meike, bmi: 22.68
    Adding Steiner War, bmi: 17.85
    Adding Lees Bay, bmi: 28.76
    Adding Rose Sophia, bmi: 20.45
    female Patients:4, male Patients:2, average BMI: 21.63
    Meier Lena 18.59
    Müller Thomas 24.07
    Weber Meike 22.68
    Steiner War 17.85
    Lees Bay 28.76
    Rose Sophia 20.45
    
    点赞 打赏 评论
  • 我真是一个渣渣 2019-01-06 23:51

    既然子类没有重新init方法, 实例子类对象时,子类调用的是父类的 init方法

    点赞 打赏 评论

相关推荐