多态:统一调用每一个类当中相同的方法,让每一个对象具备相同的接口
class SchoolPerson: """学校有哪些人""" def __init__(self, name, age): # 创建实例方法 self.name, self.age = name, age print(f"实例化学校成员:{self.name}") def say(self): print(f"姓名:{self.name} 年龄:{self.age}") class Teacher(SchoolPerson): """ 老师,继承SchoolPerson类 """ def __init__(self, name, age, salary): """ :param name: :param age: 年龄 :param salary: 工资 """ # 使用 super() + __init__ 调用父类的init方法,自动传self # 使用 super 不加括号也可以 super().__init__(name, age) self.salary = salary def say(self): # 类名 + 父类的 say(self) SchoolPerson.say(self) print(f"工资:{self.salary}") class Student(SchoolPerson): """ 学生,继承SchoolPerson类 """ def __init__(self, name, age, score): # 父类名 + init 方法,指定self SchoolPerson.__init__(self, name, age) self.score = score def say(self): SchoolPerson.say(self) print(f"分数:{self.score}") t = Teacher("可优", 17, "保密") s1 = Student("小优优", 22, "90") s2 = Student("小明", 16, "99.99") persons = [t, s1, s2] for per in persons: per.say()
*******请大家尊重原创,如要转载,请注明出处:转载自:https://www.cnblogs.com/shouhu/,谢谢!!*******