在Python中从子类调用父类构造函数

2024-05-14 06:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有课的话:

 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)

在子类中添加新参数degree时,如何让子类调用并使用父类构造函数来处理nameyear,我有点迷茫。


Tags: thenameselfobjectinitdefwith子类