因此,如果我有课:
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)
我有点失去了对如何让子类来调用和使用父类的构造函数name
和year
,同时加入了新的参数degree
在子类。
Python建议使用super()
。
Python 2:
super(Instructor, self).__init__(name, year)
Python 3:
super().__init__(name, year)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句