我真的不知道我在這里做錯了什么。當它到達輸出資料函式時,我收到錯誤“學生沒有屬性名稱”。任何想法都非常感謝。
class Person:
def __init__(self):
self.ID=""
self.name=""
self.address=""
self.Phone_number=""
self.email_id=""
self.student=self.Student()
def read_data(person):
person.ID=input("please enter ID:")
person.name=input("Please enter name:")
person.address=input("Enter address:")
person.Phone_number=input("Enter Phone Number:")
class Student:
def __init__(self):
self.class_status=""
self.major=""
def read_data(student):
student.class_status=input("Enter class status:")
student.major=input("Enter student major:")
def output_data(student):
information=(student.name " " student.ID " " student.address " " student.Phone_number " " student.class_status " " student.major "\n")
print(information)
studentFile.write(information)
def StudentDetails():
person=Person()
person.read_data()
student=person.student
student.read_data()
student.output_data()
studentDetails()
uj5u.com熱心網友回復:
外部類的屬性不會傳遞給內部類。看起來您正在嘗試對繼承關系進行建模,您可以通過使用子類而不是嵌套類來做到這一點。例如,您可以執行以下操作:
class Person:
def __init__(self):
self.ID=""
self.name=""
self.address=""
self.Phone_number=""
self.email_id=""
def read_data(person):
person.ID=input("please enter ID:")
person.name=input("Please enter name:")
person.address=input("Enter address:")
person.Phone_number=input("Enter Phone Number:")
class Student(Person):
def __init__(self):
super().__init__()
self.class_status=""
self.major=""
def read_data(self):
super().read_data()
self.class_status=input("Enter class status:")
self.major=input("Enter student major:")
def output_data(self):
information=(self.name " " self.ID " " \
self.address " " self.Phone_number " " \
self.class_status " " self.major "\n")
print(information)
def studentDetails():
student = Student()
student.read_data()
student.output_data()
studentDetails()
如果您絕對確定必須使用嵌套類,那么您試圖描述的關系就沒有意義。我可以看到類似學生 ID 類的東西是 Person 的內部類來存盤一些額外的屬性,但我認為目前描述的關系沒有多大意義。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/464271.html
