58

所以,如果我有一堂课:

 class Person(object):
'''A class with several methods that revolve around a person's Name and Age.'''

    def __init__(self, name = 'Jane Doe', year = 2012):
        '''The default constructor for the Person class.'''
        self.n = name
        self.y = year

然后这个子类:

 class Instructor(Person):
'''A subclass of the Person class, overloads the constructor with a new parameter.'''
     def __init__(self, name, year, degree):
         Person.__init__(self, name, year)

我有点迷失如何让子类调用和使用父类构造函数nameyear,同时在子类中添加新参数degree

4

1 回答 1

105

Python 建议使用super().

蟒蛇2:

super(Instructor, self).__init__(name, year)

蟒蛇 3:

super().__init__(name, year)
于 2012-09-24T01:00:53.617 回答