Skip to main content
 首页 » 编程设计

python之JPype 的 properties access shadows 方法用属性名

2025年05月04日7mayingbao

当同一个类具有 getNext() 方法时,如何访问名为 next() 的 Java 方法?

JPype 具有允许您仅使用属性名称访问 bean 属性(不带参数的 get-Methods)的功能。因此,如果您有一个带有方法 getNext() 的类,您可以使用 instance.next 从 python 中访问该 bean 属性,这在 99.9% 的情况下都很好.但是如何访问 instance.next()?如果我调用 instance.next(),我会得到一个异常,说明 bean 属性的返回类型不可调用。

请您参考如下方法:

您可能不得不求助于对底层 java 类使用反射:

# iterate through all methods 
for method in instance.__javaclass__.getMethods(): 
    # find the method matching the correct signature 
    if method.name == u'next' and len(method.parameterTypes) == 0: 
        # create a function that invokes the method on the given object 
        # without additional arguments 
        next_instance = lambda o: method.invoke(o, []) 
        break 
else: 
    raise Exception('method was not found') 
 
nxt = next_instance(instance)