但是这么写话还是并不符合我们平时写代码的规范,一般都是用super来调用,贴代码:
# 面向对象的3大特征:继承性,封装性,多态性
class Human():
sum1 = 0
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
print(self.name)
def do_homework(self):
print('parent\'s function')
class Student(Human):
def __init__(self, school, name, age):
# Human.__init__(self, name, age)
# 由于开闭原则不建议这么调用,所以改成用super调用
super(Student, self).__init__(name, age)
def do_homework(self):
super(Student, self).do_homework() # 当子类和父类有相同方法名的方法时,只会调子类的方法,可以通过super 手动调父类的方法
print('print English homework')
student1 = Student('湖南大学', '石敢当', 18)
student1.get_name()
student1.do_homework()