python 关于公有私有问题

类的公有对象可以直接 p.fun() 调用,类的私有对象必须简介的调用 将__fun() 封装到其他方法里面

class chen:

__var1=”1″

def __fun1(self):

print “私有函数”

def fun2(self):

print “公有函数”

要调用类里面的fun2可以直接实例化调用:p=chen(): p.fun2

要调用类里面的__fun1就需要通过函数来间接调用:

def fun3(self):

self.__fun1()

同理变量__var1也是需要通过函数来间接调用,因为python变量也是对象:

def fun4(self):

print self.__var1

当然 你可以通过__dict__函数来查看:

if __name__=”__main__”

print chen.__dict__

私有变量可以通过 实例._chen__var1来调用。

python 关于公有私有问题

相关文章:

你感兴趣的文章:

标签云: