代码之家  ›  专栏  ›  技术社区  ›  Compoot

属性创建和引用Python类多级继承时出错

  •  -1
  • Compoot  · 技术社区  · 7 年前

    我有以下程序,旨在演示多级继承。不幸的是 m2 eligible_for_reward 方法,我得到以下结果:

    m2=Marks()
    m2.eligible_for_reward()
    

    错误:

    Marks has no attribute totalmarks
    

    代码如下:

    # Define a class as 'student'
    class Student:
        # Method
        def getStudent(self):
            self.name = input("Name: ")
            self.age = input("Age: ")
            self.gender = input("Gender: ")
    
    #The Test Class inherits from the Student Class.
    class Test(Student):
        # Method
        def getMarks(self):
            self.stuClass = input("YearGroup/Class: ")
            print("Enter the marks of the respective subjects")
            self.literature = int(input("Literature: "))
            self.math = int(input("Math: "))
            self.biology = int(input("Biology: "))
            self.physics = int(input("Physics: "))
    
    # Note that the Marks Class inherits from the Test Class and in doing so inherits from the Student Class
    class Marks(Test):    
        # Method
        def display(self):
            print("\n\nName: ",self.name)
            print("Age: ",self.age)
            print("Gender: ",self.gender)
            print("Class Year Group: ",self.stuClass)
            self.totalmarks=self.literature+self.math+self.biology+self.physics
            print("Total Marks: ", self.literature + self.math + self.biology + self.physics)
    
        def eligible_for_reward(self):
            if self.totalmarks>=90 and self.age>13:
                print("You are eligible for the fabulous school sponsored trip to London")
            else:
                print("Sorry, you are not eligible and so are going nowhere....!")
    

    更新:

    我也试过以下几点,那就是定义自我。totalmarks在相关方法中,但也出现了一个错误: 未定义名称“totalmarks”

    def eligible_for_reward(self):
            self.totalmarks=totalmarks
            if self.totalmarks>=90 and self.age>13:
                print("You are eligible for the fabulous school sponsored trip to London")
            else:
                print("Sorry, you are not eligible and so are going nowhere....!")
    

    此外,我也试过了

        m2=Marks()
        m2.display()
        m2.eligible_for_reward()
    

    AttributeError: 'Marks' object has no attribute 'name'
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Alexey    7 年前

    您定义属性 self.totalmarks 在里面 display() 方法 Marks

    因此,当您为奖励()调用合格的_时,此时未定义totalmarks。因此,您可以首先调用display()方法来解决此问题:

    m2=Marks()
    m2.dispay()
    m2.eligible_for_reward()
    

    self.literature self.math self.biology self.physics 目前也没有定义。

    你必须添加 ()方法调用所有类以在内部调用super(),并正确实例化该类,如下所示:

    class Student:
        def __init__(self):
            self.name = input("Name: ")
            self.age = input("Age: ")
            self.gender = input("Gender: ")
    
    class Test(Student):
        def __init__(self):
            super(Test, self).__init__()
            self.stuClass = input("YearGroup/Class: ")
            print("Enter the marks of the respective subjects")
            self.literature = int(input("Literature: "))
            self.math = int(input("Math: "))
            self.biology = int(input("Biology: "))
            self.physics = int(input("Physics: "))
    
    class Marks(Test):    
        def __init__(self):
            super(Marks, self).__init__()
            self.total_marks = self.literature + self.math + self.biology + self.physics
    
        def display(self):
            print("\n\nName: ",self.name)
            print("Age: ",self.age)
            print("Gender: ",self.gender)
            print("Class Year Group: ",self.stuClass)
    
            print("Total Marks: ", self.literature + self.math + self.biology + self.physics)
    
        def eligible_for_reward(self):
            if self.total_marks>=90 and self.age>13:
                print("You are eligible for the fabulous school sponsored trip to London")
            else:
                print("Sorry, you are not eligible and so are going nowhere....!")
    

    您可以按如下方式使用它:

    marks = Marks()
    marks.eligible_for_reward()
    marks.display()
    
        2
  •  1
  •   Igl3    7 年前

    正如我在评论中提到的,我不认为你子类化的方式有什么原因。所以我把它作为一个班的学生放在一起,就像这样:

    class Student:
        # Method
        def __init__(self):
            self.name = input("Name: ")
            self.age = int(input("Age: "))
            self.gender = input("Gender: ")
    
        def setMarks(self):
             self.stuClass = input("YearGroup/Class: ")
             print("Enter the marks of the respective subjects")
             self.literature = int(input("Literature: "))
             self.math = int(input("Math: "))
             self.biology = int(input("Biology: "))
             self.physics = int(input("Physics: "))
             self.totalmarks=self.literature+self.math+self.biology+self.physics
    
        def display(self):
             print("\n\nName: ",self.name)
             print("Age: ",self.age)
             print("Gender: ",self.gender)
             print("Class Year Group: ",self.stuClass)
             self.totalmarks=self.literature+self.math+self.biology+self.physics
             print("Total Marks: ", self.literature + self.math + self.biology + self.physics)
    
        def eligible_for_reward(self):
             if self.totalmarks>=90 and self.age>13:
                 print("You are eligible for the fabulous school sponsored trip to London")
             else:
                 print("Sorry, you are not eligible and so are going nowhere....!")
    

    示例用法:

    s1 = Student()
    >>> Name: Hans
    >>> Age: 17
    >>> Gender: m
    s1.setMarks()
    >>> YearGroup/Class: 2017
    >>> Enter the marks of the respective subjects
    >>> Literature: 11
    >>> Math: 14
    >>> Biology: 42
    >>> Physics: 10
    
    s1.display()
    
    
    >>> Name:  Hans
    >>> Age:  17
    >>> Gender:  m
    >>> Class Year Group:  2017
    >>> Total Marks:  77
    
    s1.eligible_for_reward()
    >>> Sorry, you are not eligible and so are going nowhere....!
    
        3
  •  1
  •   Abhijeetk431    7 年前

    以下是编辑最少的工作代码,希望这能帮助您:-

    class Student:
        # Method
        def getStudent(self):
            self.name = input("Name: ")
            self.age = int(input("Age: "))
            self.gender = input("Gender: ")
    
    #The Test Class inherits from the Student Class.
    class Test(Student):
        def __init__(self):
            super().__init__()
            super().getStudent()
        # Method
        def getMarks(self):
            self.stuClass = input("YearGroup/Class: ")
            print("Enter the marks of the respective subjects")
            self.literature = int(input("Literature: "))
            self.math = int(input("Math: "))
            self.biology = int(input("Biology: "))
            self.physics = int(input("Physics: "))
    
    # Note that the Marks Class inherits from the Test Class and in doing so inherits from the Student Class
    class Marks(Test):
        def __init__(self):
            super().__init__()
            super().getMarks()
        # Method
        def display(self):
            print("\n\nName: ",self.name)
            print("Age: ",self.age)
            print("Gender: ",self.gender)
            print("Class Year Group: ",self.stuClass)
            self.totalmarks=self.literature+self.math+self.biology+self.physics
            print("Total Marks: ", self.literature + self.math + self.biology + self.physics)
    
        def eligible_for_reward(self):
            if self.totalmarks>=90 and self.age>13:
                print("You are eligible for the fabulous school sponsored trip to London")
            else:
                print("Sorry, you are not eligible and so are going nowhere....!")
    
    
    
    mrk = Marks()
    mrk.display()
    mrk.eligible_for_reward()