
继承
class Person: #默认继承了object def __init__(self,name,age): self.name = name self.age = age def show(self): print(f'大家好,我叫{self.name},我今年{self.age}岁') # Student 继承 Person类 class Student(Person): #编写初始化的方法 def __init__(self,name,age,stuno): super().__init__(name,age) #调用父类的初始化方法 self.stuno=stuno class Doctor(Person): def __init__(self,name,age,department): super().__init__(name, age) self.department=department #创建第一个子类对象 stu=Student('cmm',20,'1001') stu.show() doctor=Doctor('zyy',32,'wk') doctor.show()
输出
大家好,我叫cmm,我今年20岁 大家好,我叫zyy,我今年32岁

(仅供参考)
Comments NOTHING