#Python继承的特点
总是从某个类继承
class MyClass(object):
pass
不要忘记调用 super().init
class MyClass(object):
def __init__(self,args):
super(SubClass,self).__init__(args)
pass
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self, name, gender, course):
super(Teacher,self).__init__(name,gender)
self.course = course
t = Teacher('Alice', 'Female', 'English')
print t.name
print t.course
一定要用 super(Teacher, self).init(name, gender) 去初始化父类,否则,继承自 Person 的 Teacher 将没有 name 和 gender。
python 中的多态
网友评论