我真的不知道我哪里做错了。我得到的错误是"学生没有属性名"当它到达输出数据函数时。如有任何意见,不胜感激。
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 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()
如果你绝对确定你必须使用嵌套类,那么你试图描述的关系是没有意义的。我可以看到像student ID类这样的东西是Person的内部类,用于存储一些额外的属性,但我认为目前描述的关系没有多大意义。