class Human: def __init__(self, n, a): self.name = n self.age = a print("Human类的初始化方法被调用") def infos(self): print("姓名:", self.name) print("年龄:", self.age) class Student(Human): def __init__(self, n, a, s=0): self.score = s super(Student,self).__init__(n,a) print("Student的初始化被调用") def infos(self): super().infos() #由于父类中有infos方法,因此利用super()方法进行调用 print("成绩:", self.score) s1 = Student("zengsf", 15, 90) s1.infos() 输出结果: Human类的初始化方法被调用 Student的初始化被调用 姓名: zengsf 年龄: 15 成绩: 90
原文地址:https://www.cnblogs.com/zengsf/p/9532361.html
时间: 2024-11-05 15:47:50