class Person(object): #Person继承object类 def __init__(self,name,age): self.name=name self.age=age def info(self): print(self.name,self.age) class Student(Person): def __init__(self,name,age,stu_no): super().__init__(name,age) self.stu_no=stu_no def info(self): #输出与父类不一样的数据,方法重写 super().info() #调用父类数据 print(self.stu_no) #输出与父类不一样的数据,方法重写 class Teacher(Person): def __init__(self,name,age,teachofyear): super().__init__(name,age) self.teachofyear=teachofyear def info(self): #输出与父类不一样的数据,方法重写 super().info() #调用父类 print('教龄',self.teachofyear) #输出与父类不一样的数据,方法重写 stu=Student('张三',20,'1001') teacher=Teacher('李四',34,10) stu.info() print('----------------------') teacher.info()
E:\PycharmProjects\pythonProject\venv\Scripts\python.exe E:/PycharmProjects/pythonProject/demon1/demo45.py 张三 20 1001 ---------------------- 李四 34 教龄 10 进程已结束,退出代码0