外部 class 属性未传递给内部 class 属性 python 继承

Outer class attributes not passing to inner class Attributes for python inheritance

我真的不知道我做错了什么。当它到达输出数据函数时,我收到错误“student has no attribute name”。非常感谢任何想法。

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

外部 class 的属性不会传递给内部 class。看起来您正在尝试对继承关系建模,您可以通过使用 subclassing 而不是嵌套 classes 来实现。例如,您可以执行如下操作:

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

如果您绝对确定必须使用嵌套 class,那么您试图描述的关系就没有意义。我可以看到像学生 ID class 这样的东西是 Person 的内部 class 来存储一些额外的属性,但我认为目前描述的关系没有多大意义。