Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
面向对象Python问题_Python_Oop - Fatal编程技术网

面向对象Python问题

面向对象Python问题,python,oop,Python,Oop,我最近学习了面向对象编程,我遇到了这些问题 当我运行此代码时,出现了以下错误 我怎样才能修好它 class Grade: def __init__(self,**kwargs): self.information = kwargs def grade(self): g = self.information['grade'] print(g , "Grade") def students(self):

我最近学习了面向对象编程,我遇到了这些问题 当我运行此代码时,出现了以下错误 我怎样才能修好它

class Grade:
    def __init__(self,**kwargs):
        self.information = kwargs
    def grade(self):
        g = self.information['grade']
        print(g , "Grade")
    def students(self):
        s = self.information['students']
        print("students : ",s)
    def chairs(self):
        c = self.information['chairs']
        print("Chairs : ",c)
class school(Grade):
    def __init__(self,**kwargs):
        self._grades = kwargs
    def classes(self):
        print("YOU HAVE IN THE SCHOLL ",self._grades["classes"],"classes")  
    def students(self):
        super().students()   
first = Grade(grade="first" , students=20,chairs=15)
fir_g = school(classes=9)
fir_g.students()
    
错误:

Traceback (most recent call last):
  File "c:\Users\hp\Desktop\python\OOP.py", line 27, in <module>
    fir_g.students()
  File "c:\Users\hp\Desktop\python\OOP.py", line 19, in students
    super(school,self).students()
  File "c:\Users\hp\Desktop\python\OOP.py", line 8, in students
    s = self.information['students']
AttributeError: 'school' object has no attribute 'information'
回溯(最近一次呼叫最后一次):
文件“c:\Users\hp\Desktop\python\OOP.py”,第27行,在
飞行学员()
文件“c:\Users\hp\Desktop\python\OOP.py”,第19行,在学生中
超级(学校,自我)学生()
文件“c:\Users\hp\Desktop\python\OOP.py”,第8行,在学生中
s=自我信息[‘学生’]
AttributeError:“学校”对象没有属性“信息”

如何解决此问题?

我相信您只需要为继承的类运行
\uuu init\uuu
,因为这就是您定义
信息的地方:

class school(Grade):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self._grades = kwargs
然后应该定义
信息。始终运行继承类的
\uuuu init\uuu
方法很重要,因为它不是自动运行的

请注意,这意味着您也需要将
年级
的任何参数传递到学校。虽然,这似乎不是你想要的结构。当然,
学校
会高于年级,那么为什么它会继承自
年级
?我强烈建议你重新思考你的课程结构,以及你希望他们如何相互作用。也许你想在
school
中添加一个
Grade
对象,或者类似的东西