类是私有属性

class People(object):
    color = 'yellow'  #这就是一个公有属性,可以在类里和类外调用
    __age = 30       #这就是一个私有属性,只能在类里调用。
    def think(self):
        self.color = "black"
        print ("i am a %s" % self.color)
        print ("i am a thinker")
        print(self.__age)    #调用age
ren = People()
print(ren.color)
ren.think()
print ("-"*66)
print(ren._People__age)

输出:

yellow
i am a black
i am a thinker
30
------------------------------------------------------------------
30
posted @ 2022-10-22 19:56  ty1539  阅读(16)  评论(0编辑  收藏  举报