简体   繁体   English

外部 class 属性未传递给内部 class python 的属性 inheritance

[英]Outer class attributes not passing to inner class Attributes for python inheritance

I don't really know what I have done wrong here.我真的不知道我在这里做错了什么。 I get the error "student has no attribute name" when it gets to the output data function. Any ideas are greatly appreciated.当它到达 output 数据 function 时,我收到错误“学生没有属性名称”。非常感谢任何想法。

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

The attributes of an outer class aren't passed to an inner class. It looks like you're trying to model an inheritance relationship, which you can do by using subclassing rather than nesting classes.外部 class 的属性不会传递给内部 class。看起来您正在尝试 model 和 inheritance 关系,您可以通过使用子类而不是嵌套类来实现。 For example, you could do something like the following:例如,您可以执行以下操作:

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

If you are absolutely sure that you must use a nested class, then the relationship you're trying to describe doesn't make sense.如果您绝对确定必须使用嵌套的 class,那么您试图描述的关系就没有意义。 I could see something like a student ID class being an inner class of Person to store some additional attributes, but I don't think the relationship as currently described makes much sense.我可以看到像学生 ID class 这样的东西是 Person 的内部 class 来存储一些额外的属性,但我认为目前描述的关系没有多大意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM