2

next()当同一个类有方法时,如何访问命名的 JavagetNext()方法?

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

4

2 回答 2

1

您可能不得不求助于对底层 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)
于 2013-10-01T20:12:06.363 回答
0

它固定在 originell 的 jpype 叉 https://github.com/originell/jpype/commit/0e6067f2eec78f6c697b3714f467142fa9b58243

于 2014-02-12T14:07:37.613 回答