python面向对象之私有和继承
- 父类中的 私有方法、属性,不会被子类继承
- 可以通过调用继承的父类的共有方法,间接的访问父类的私有方法、属性
class Animal:
def __init__(self):
self.age = 0 # 公有属性可以被继承
self.__type = "动物" # 私有属性不能被继承
def __eat(self): # 私有方法不能被继承
print("吃东西")
class Dog(Animal):
def do(self):
print(self.age) # 0 公有属性可以被继承
# print(self.__type) # 报错 私有属性不能被继承
# self.__eat() # 私有方法不能被继承
dog1 = Dog()
dog1.do()