从jython调用Java方法的右重载 [英] Calling right overload of the java method from jython
问题描述
我正在使用Java库,该库在我使用的类中具有重载的方法.
I am using java library, that has overloaded methods in the class that I use.
JAVA:
void f(float[]);
void f(Object[]);
现在,我从jython调用此类,并且我想调用Object []重载.问题在于python将我的数组视为浮点数组,因此调用了错误的重载方法.
Now I call this class from jython, and I want to call the Object[] overload. The problem is that python sees my array as array of floats, and therefore calls the wrong overload methods.
JYTHON:
f([[1, 1.0])
如何强制执行Object []方法?
How to force the Object[] method to be executed?
推荐答案
我花了很多时间才能找到答案,所以我决定将问题和答案一起发布.
It took me considerable amount of time to find out, so I decided to post a question together with the answer.
Jython文档告诉您,为了强制调用右重载,您应该在调用之前手动将参数强制转换为java对象:
Jython documentation tells that in order to to force a call of right overload you should cast arguments to java objects manually before the call:
from java.lang import Byte
foo(Byte(10))
但是,这不适用于Java数组.
However that doesn't work with java arrays.
http://www.jython.org/archive/22/userguide.html#java-arrays
可以在jython中创建Java数组.例如,以下代码将在jython java类型为int []
It's possible to create java arrays in jython. For example the following code will create in jython java array of type int[]
from jarray import array
array(python_array, 'i')
您可以像这样创建Object [],并强制java调用正确的重载.
You can create Object[] like this, and force java to call the right overload.
from jarray import array
from java.lang import Object
oa = array(python_array, Object)
f(oa)
这篇关于从jython调用Java方法的右重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!