# 测试继承
class Person:
def __init__(self, name, age):
self.name = name
self.__age = age # 私有属性
def say_age(self):
print("我不知道年龄啊")
class Student(Person):
def __init__(self, name, age, score):
Person.__init__(self, name, age) # 必须用显式地调用父类初始化方法,不然解释器不会调用
self.score = score
# Student-->Person-->object类
# print(Student.mro())
s = Student("小七", 18, 60)
s.say_age()
print(s.name)
print(s._Person.__age) # 调用父类的私有属性
C:\Users\Administrator\PycharmProjects\pycharm001\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/pycharm001/function001/面向对象编程/09.继承.py
我不知道年龄啊
小七
Traceback (most recent call last):
File "C:\Users\Administrator\PycharmProjects\pycharm001\function001\面向对象编程\09.继承.py", line 23, in <module>
print(s._Person.__age) # 调用父类的私有属性
AttributeError: 'Student' object has no attribute '_Person'
Process finished with exit code 1
老师,为什么子类没有继承到父类的属性啊