首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python的getter/setter,该怎么处理

2013-01-20 
python的getter/settergetsettest.pyimport test

python的getter/setter
===========================
getsettest.py
===========================
import test

test1 = test.Test()
test1.set__foo = 100
a=0
a = test1.get__foo
print a

test2 = test.Test()
b = test2.get__foo
print b

===========================
test.py
===========================
class Test(object):

def __init__(self):
self.__foo = 0

def get__foo(self):
return self.__foo

def set__foo(self):
self.__foo = foo

===========================
输出
===========================
<bound method Test.get__foo of <test.Test object at 0x00C03A70>>
<bound method Test.get__foo of <test.Test object at 0x00C03A30>>


请教这应该怎么写啊getter/setter...
像java那样写明显不对头啊= =!
[解决办法]
import test

test1 = test.Test()
test1.set__foo() = 100
a=0
a = test1.get__foo()
print a

test2 = test.Test()
b = test2.get__foo()
print b

热点排行