'''
class Person(object):
def __init__(self, name, age):
# self.name = name
self.__name = name # __name即私有属性,设置 _Person__name = ”yuan“
self.age = age
self._score = 100
def get_name(self):
print(self.__name)
def __foo(self): # _Person__foo
print("foo")
class Teacher(Person):
def bar(self):
self.__foo() # self._Teacher__foo
p1 = Person("xi",22)
p1.get_name() #xi
p1.__foo() # foo
p1._Person__foo() # foo
p1.bar() # Teacher无__foo变量 (AttributeError: 'Person' object has no attribute 'bar')
t1 = Teacher("xi", 23)
t1.bar() # Teacher无__foo变量 (AttributeError: 'Teacher' object has no attribute '_Teacher__foo')
'''
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/246190.html