我有一个名为StudentBody的父类和一个名为MathStudentBody的子类。我的问题是我怎样才能解释这个孩子的班级,从而找出班上的学生总数?我想我们得找出被创造的物体的总数?有人能给我指出正确的方向吗
class StudentBody:
count = 0
def __init__(self, name,gender,year,gpa):
self.name = name
self.gender = gender
self.year = year
self.gpa = gpa
self.count+= 1
def IsFreshman(self):
print "I am the StudentBody method"
if self.year == 1:
return True
else :
return False
def countTotal(self):
return self.count
class MathStudentBody(StudentBody):
def __init__(self,name,gender,year,gpa,mathSATScore):
#super(MathStudentBody,self).__init__(name,gender,year,gpa)
StudentBody.__init__(self,name,gender,year,gpa)
self.MathSATScore = mathSATScore
def IsFreshman(self):
print "I am the MathStudentBody method"
def CombinedSATandGPA(self):
return self.gpa*100 + self.MathSATScore
def NumberOfStudents(self):
return
你的意思是这样的(把代码精简到最低限度…)
请注意,我将对类变量的访问权限更改为
StudentBody.count
(从self.count
),如果您是只读的,则可以这样做。但是一旦你给self.count
赋值,这个变化只会影响实例self
,而不会影响类)。在MathStudentBody
中调用super().__init__()
也会增加StudentBody.count
。你知道吗(^{} 。。。咯咯笑!)你知道吗
相关问题 更多 >
编程相关推荐