-1
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 类访问 courseInfo 类的方法,而不创建进一步的子类?

4

4 回答 4

0

你的问题不是很清楚,这有帮助吗?

class abc(object):

    def __init__(self, courses):
        [...]
        self.my_courses = {course_name: courseInfo(course_name) 
                           for course_name in courses}

    def setGrade(self, grade, course="6.01x"):
        course = self.my_courses[course]
        course.setGrade(grade)

    def getGrade(self, course="6.02x"):
        course = self.my_courses[course]
        return course.getGrade()

而不是 a list,我将您的课程存储在 adict中,以便能够按名称查找它们。还有其他方法可以做到这一点,但这既简单又优雅(并且比每次都遍历列表更有效)。

于 2013-05-07T11:02:47.740 回答
0

这似乎是简单的组合,与“访问基类”无关。您的courses属性包含用户的所有课程,每个课程都有自己的setGradegetGrade方法。因此,您只需确定该列表中的相关课程,并调用其方法。就像是:

for c in self.myCourses:
    if c.courseName == course:
        c.setGrade(grade)

我不知道您是否可以控制其结构abc及其__init__方法,但如果您将课程存储为按名称键入的字典而不是列表,这会更容易。

于 2013-05-07T11:03:22.810 回答
0

问题是这种格式,我要求一种方法来访问任一基类的成员,而无需创建任何进一步的子类

好的,既然你有所有courseInfoin的引用myCourses,你可以这样做:

def setGrade(self, grade, course="6.01x"):
    for i in self.myCourses:
        if i.courseName == course:
            i.setGrade(grade)

但是,您确实应该进行适当的继承。

于 2013-05-07T11:04:06.857 回答
0

您在编写 Python 时正在使用 C++ 编写和思考。

如果我有一个例子,courseInfo那么c在程序中的任何地方我都可以执行以下任何操作:

c.setGrade('a')
g = c.getGrade()

甚至

c.grade = 'this is not really a grade'

类实例的每个成员都是“公共的”,Python 封装的工作原理是“我们都是成年人”,所以除非我允许,否则不要触碰我的隐私。还有另一种约定,属性名称开头的下划线警告您它是私有属性并且您真的不应该触摸它。如果courseInfo

self._grade - 'No Grade'

那么你知道你访问_grade是有风险的。这在 C++ 中也是如此,只是更难并且涉及强制转换。

人们稍后可能会回答,这self.__grade 不同的处理方式,但它不会使其私有化,作为初学者,您最好避免构造,直到您了解为什么 getter 和 setter 在 Python 中远不如在 Java 和 C++ 中常见。

于 2013-05-07T10:53:43.987 回答