我正在尝试在 jython v2.1 中为 java 对象(baseClient)实现包装器/代理类。除非遇到以下语句,否则一切似乎都正常:
if __client != None # __client is an instance of the ClientProxy class
raise AttributeError(attr)
被调用__getattr__()
,因为self.__baseClient
没有__ne__
属性。重要的是要提到我无法升级,因为 jython 是系统的一部分。有没有办法解决这个问题?
class ClientProxy:
def __init__(self, baseClient):
self.__baseClient = baseClient
self.__initialised = 1
def __getattr__(self, attr):
if not self.__dict__.has_key('_ClientProxy__initialised'):
raise AttributeError(attr)
else:
if hasattr(self.__baseClient, attr):
return getattr(self.__baseClient, attr)
else:
raise AttributeError(attr)
def __setattr__(self, attr, val):
if not self.__dict__.has_key('_ClientProxy__initialised'):
self.__dict__[attr] = val
return
if hasattr(self.__baseClient, attr):
self.__baseClient.__setattr__(attr, val)
else:
self.__dict__[attr] = val
非常感谢!