Dan*_* Jr 44 python inheritance
所以,如果我有一个班级:
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
Run Code Online (Sandbox Code Playgroud)
然后这个子类:
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)
Run Code Online (Sandbox Code Playgroud)
我有点迷失了如何让子类调用并使用父类构造函数,name并在子类中year添加新参数degree.
vin*_*ehl 71
Python建议使用super().
Python 2:
super(Instructor, self).__init__(name, year)
Run Code Online (Sandbox Code Playgroud)
Python 3:
super().__init__(name, year)
Run Code Online (Sandbox Code Playgroud)