属性创建和引用Python类多级继承时出错
我有下面的程序,试图演示多级继承。不幸的是,在调用属性创建和引用Python类多级继承时出错,python,class,inheritance,Python,Class,Inheritance,我有下面的程序,试图演示多级继承。不幸的是,在调用m2和有资格获得奖励方法时,我得到以下信息: 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: ")
m2
和有资格获得奖励
方法时,我得到以下信息:
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....!")
更新:
我还尝试了以下方法,即在相关方法中定义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'
您可以在
display()
方法的Marks
类中定义属性self.totalmarks
因此,当您为奖励()调用合格的奖励时,此时未定义totalmarks。因此,您可以先调用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....!")
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....!")
您可以按如下方式使用它:
marks = Marks()
marks.eligible_for_reward()
marks.display()
正如我在评论中提到的那样,我不认为您子类化的方式有什么原因。所以我把它放在一个班级里,就像这样:
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....!")
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....!
以下是经过最少编辑的工作代码,希望对您有所帮助:-
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()
只有在调用
Marks.display()
时,才能设置totalmarks
。您没有调用Marks.display()
,因此该属性不存在。非常感谢。根据下面的建议答案,我尝试了这个方法,但出现了一个错误。请查看display()
需要哪些属性,以及这些属性的设置位置。我可以看到它们是由用户在上一个类中输入的,但不确定如何在新的类/方法中再次声明它们。这就是我需要的帮助和指导!在Marks类中,我可以简单地初始化init中的属性吗?例如self.literature=等。这些属性完全不相关,但是您的继承树(从设计角度来看)完全错误。继承在语义上是一种“is a”关系(如果B从a继承,那么B就是a)。很明显,分数不是考试,学生也不是。。。这里要描述的关系是“has a”(或“has many”)关系,它们是通过聚合实现的。理想情况下,我希望正确定义它,这样就不必首先调用display方法。这是如何做到的?请参阅我的编辑。另外,使用您的解决方案,我现在得到了以下错误:AttributeError:“Marks”对象没有属性“name”。解决方案需要解释如何最好地实现这一点,这样它才能工作!请注意,display()
使用其他方法设置的属性。你不能只告诉他们运行display()
。谢谢你,阿列克谢-我马上就去试试。你能补充一下你的答案吗,“超级”部分到底做了什么。我不认为在创建类时需要显式地使用“super”来扩展/继承超类……我不清楚这一点。我会将input()
调用移出类。您希望能够从不同的源创建这样一个类,而不仅仅是用户输入。我正在进行子分类以演示多级继承。关于如何用你的答案证明这一点的任何建议。这是我这项任务的重点。很多人都感谢他们尝试将学生分类为本科生、毕业生等。正如布鲁诺·德舒利尔(bruno desthuilliers)在评论中所说的那样,这种关系应该是“a”。所以本科生是学生。它继承了诸如姓名、年龄等标准属性。但是可以有额外的字段和函数,只有本科生才有。因此,所有类继承的关系都必须是is-a,这是一条严格的规则。(例如,如果B继承自A,则B为A)。。。?我从来不知道这件事。在游戏中,玩家不是从分数或世界级继承的吗?我想澄清一下这一点是的,这实际上就是继承的概念。不,我不这么认为,玩家可能有一个Score属性,它本身可以是一个类,但是Score不会从玩家继承。但是继承是关于继承属性和方法,不是吗?e、 玩家可以继承分数的属性,比如当前的分数或健康状况。如果某个属性是另一个类的属性,则该属性不具有score属性,这也是继承的一个示例。这就是一些教程似乎教它的方式。只是添加了超级类的init函数,并触发了一些其他必要的函数。将年龄转换为int并调用display函数初始化totalmarks变量。