class Animal(object):
def __init__(self,name,age):
self.name = name
self.age = age
self.__money = 1000
def eat(self):
print(self.name + '正在吃东西')
def __test(self):
print('我是Animal里的__test方法')
class Person(Animal):
def __demo(self):
print('我是Person类里的私有方法 __deemo')
p = Person('张三',18)
print(p.name)
p.eat()
p._Person__demo() # 自己类里定义的是私有方法 对象名._类名__私有方法名()
p._Animal__test() # 可以通过 对象名.父类名__私有方法1调用()
# p._Person.__test() # 父类的私有方法,子类没有继承
# 私有1属性和方法,子类不会继承
print(p._Animal__money)
结果
张三
张三正在吃东西
我是Person类里的私有方法 __deemo
我是Animal里的__test方法
1000
Process finished with exit code 0