目录
类成员的继承和重写
=> 成员继承
子类继承了父类除构造方法之外的所有成员;
=> 方法重写
子类可以重新定义父类中的方法, 这样就会覆盖父类的方法, 也称为”重写”
#类的继承和重写
class Person:
def __init__(self,name,age):
self.name = name
self.__age = age
def say_age(self):
print('我的年龄是:',self.__age)
def say_introduce(self):
print("我的名字是:",self.name)
class Student(Person):
def __init__(self,name,age,score):
Person.__init__(self,name,age)
self.score = score
def say_introduce(self): #重写父类的该方法
'''重写父类的办法'''
print("报告老师,我的名字的是:",self.name)
s1 = Student('张三',19,99)
s1.say_age() #继承
s1.say_introduce()#继承
输出:
D:\wwwroot\pyiteam\venv\Scripts\python.exe D:/wwwroot/pyiteam/mypro_obj/mypy02.py
我的年龄是: 19
报告老师,我的名字的是: 张三
Process finished with exit code 0