python 类的私有方法例子

#coding=utf-8

class Person(object):
    id=12
    def __init__(self,name):
        self.name=name
        self.__inName='ads'

    def __setId(self,id):
        Person.id=id*2

    def getId(self):
        self.__setId(18)
        return Person.id

p=Person("prel")
print p.getId()
print "call private function from out of class"
print p.__setId()#报错

 

c:\Python27\Scripts>python task_test.py
36
call private function from out of class
Traceback (most recent call last):
  File "task_test.py", line 19, in <module>
    print p.__setId()
AttributeError: 'Person' object has no attribute '__setId'

 

posted @ 2018-04-10 12:41  夏晓旭  阅读(161)  评论(0编辑  收藏  举报