在 Python 中从另一个类调用方法时出现问题

问题描述 投票:0回答:1

我是Python新手,我似乎无法用我的程序解决问题。


import datetime     

class Pet:
    def __init__ (self, name, year_born, species):
        self.__name = name             
        self.__year_born = year_born    
        self.species = species     

    def __compute_age (self):          
        today = datetime.datetime.now()
        return today.year - self.__year_born    

    def get_pet_details (self):        
        pet_details = {"Name" : self.__name, "species" : self.species, "Age" : self.__compute_age()}
        return pet_details
        

class VetHospital:
    def __init__ (self, name):
        self.__name = name             
        self.__patients = []          

    def get_name (self):            
        return self.__name

    def admit_patient (self):    
        pet_name_input = input('\n'"Please enter the pet/patient's name to be admitted: ")                           
        pet_year_born_input = int(input("Please enter the {}'s year of birth: " .format(pet_name_input)))        
        pet_species_input = input("Please enter the {}'s species type: " .format(pet_name_input))
        pet = Pet(pet_name_input, pet_year_born_input, pet_species_input)
        pet.get_pet_details()
        pet_details = {"Name" : pet_name_input, "species" : pet_species_input, "Age" : pet._Pet__compute_age()}              
        self.__patients.append(pet_details)     
        print('\n'"{} was successfully admitted into {}.".format(pet_name_input, self.__name))
        
    def discharge_patient (self): 
        pet_name_input = input('\n'"Please enter the patient's name to be discharged: ")                               
        for pet in self.__patients:                                                                        
            if pet._Pet__name == pet_name_input.lower():                                                            
                self.__patients.remove(pet)                                                                 
                print("{} has been discharged.".format(pet_name_input))                                               
                return
        print("There is no patient named {} admitted into {}.".format(pet_name_input, self.__name))

    def get_patient_info (self): 
        pet_name_input = input('\n'"Please enter the patient's name: ")                               
        for pet in self.__patients:                                                                        
            if pet.pet._Pet__name == pet_name_input.lower():                                                            
                print(pet._Pet_get_pet_details())                                               
                return
        print("There is no patient named {} admitted into {}.".format(pet_name_input, self.__name))

    def get_all_patients_info (self):  
        all_patient_info = []
        for pet in self.__patients:
            all_patient_info.append(self.get_patient_info(pet))
        if not self.__patients:                          
            print('\n'"Currently, there are no patients admitted in {}." .format(self.__name))  
        else:
            return all_patient_info
    
vet_hospital = VetHospital(input("Vet Name: "))
vet_hospital.admit_patient()
vet_hospital.discharge_patient()
vet_hospital_blank = VetHospital("h")
vet_hospital_blank.get_patient_info()
vet_hospital_blank.get_all_patients_info()

有两个问题:

  1. 问题出在这一行:
    pet_details = {"Name" : pet_name_input, "species" : pet_species_input, "Age" : pet._Pet__compute_age()}

我希望它与 Pet 类中的 pet_details 对象具有相同的属性,但我不知道如何使用 pet 实例调用 __compute_age 方法。

  1. 我想用这段代码实现的总体目标是确保用户的输入将保存在 self.__ Patients 和 pet_details 列表中,这样当我使用 get_Patent_info 方法调用患者的姓名时,它将显示pet_details 列表中的患者信息。
python list methods instance class-method
1个回答
0
投票
  1. 将函数 ComputeAge 设置为静态函数。然后就可以在没有类实例的情况下获得它。
  2. 考虑继承。您的宠物类可以继承自 VetHospital 并使用父属性。
© www.soinside.com 2019 - 2024. All rights reserved.