'''私有属性
说明:
1.私有属性不能在外面通过实例调用
2.只能够在类里面自己来调用
3.私有属性对外面是隐藏的'''
classAnimal(object):
hobbie= "meat"
def __init__(self,name):
self.name=name
self.__num =None
@propertydeftotal_players(self):print(self.__num)
@total_players.setterdeftotal_players(self,num):
self.__num =numprint("total players:",self.__num)
d= Animal("Eric")print(d.total_players) #None
d.total_players = 4
print(d.total_players) #total players: 4
d.__num = 9 #这样赋值(不会给类里面的__num私有变量赋值),只是在d对象中重新创建了一个__num属性,值等于9
print(d.__num) #9
#其实用(_类名__属性名)还是能调用私有属性
print("OUT:",d._Animal__num) #OUT: 4
最后
以上就是感性金毛最近收集整理的关于python中属于私有属性的是_Python类中私有属性的全部内容,更多相关python中属于私有属性内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复