本文主要是介绍Pyuthon私有方法练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
'''
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')
'''
这篇关于Pyuthon私有方法练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!