私有成员&共有成员
class Test:
def __init__(self, name, age):
self.name = name #共有
self.__age = age #私有,外部无法通过对象直接调用
def show(self):
return self.__age #通过内部方法访问
@property
def age(self):
return self.__age #静态方法访问
obj = Test("frankshi",25)
# print(obj.name)
# print(obj.__age) #AttributeError: 'Test' object has no attribute '__age'
# print(obj.show())
# print(obj.age)
私有方法&共有方法
class Test1:
def __init__(self):
pass
def __show(self): #私有方法
print(123)
obj = Test1()
obj._show() #AttributeError: 'Test1' object has no attribute '_show'