class courseInfo(object):
def __init__(self, courseName):
self.courseName = courseName
self.psetsDone = []
self.grade = "No Grade"
def setGrade(self, grade):
if self.grade == "No Grade":
self.grade = grade
def getGrade(self):
return self.grade
class abc(object):
def __init__(self, courses):
self.myCourses = []
self.Pset = []
self.grade = {}
for course in courses:
self.myCourses.append(courseInfo(course))
def setGrade(self, grade, course="6.01x"):
"""
grade: integer greater than or equal to 0 and less than or
equal to 100
course: string
This method sets the grade in the courseInfo object named
by `course`.
If `course` was not part of the initialization, then no grade is
set, and no error is thrown.
The method does not return a value.
"""
def getGrade(self, course="6.02x"):
"""
course: string
This method gets the grade in the the courseInfo object
named by `course`.
returns: the integer grade for `course`.
If `course` was not part of the initialization, returns -1.
"""
xyz = abc( ["6.00x","6.01x","6.02x"] )
xyz.setGrade(100)
print xyz.getGrade(course="6.01x")
print Xyz.getGrade(course="6.02x")
問題は、Python で別の基本クラスからある基本クラスのメンバーにアクセスする方法です。ここで、さらにサブクラスを作成せずに、abc class から courseInfo クラスのメソッドにアクセスしますか?