我有以下程序试图演示多级继承。不幸的是,在调用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....!")
更新:
我也尝试过以下方法,即相关方法中的definig self.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'
答案 0 :(得分:1)
您可以在self.totalmarks
类
display()
方法中定义属性Marks
因此,当您调用qualified_for_reward()时,此时不会定义总标记。所以你可以先调用display()方法来解决这个问题:
m2=Marks()
m2.dispay()
m2.eligible_for_reward()
但这不会起作用,因为
目前还没有定义self.literature
self.math
self.biology
self.physics
。
您必须将 init ()方法添加到所有类中以调用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()
答案 1 :(得分:1)
正如我在评论中提到的,我没有看到你进行子类化的原因。所以我把它作为一个班级学生放在一起这样:
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....!
答案 2 :(得分:1)
这是使用最少编辑的工作代码,希望这可以帮助您: -
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()