HI,
假设我有一个 Java 接口 B,类似这样。 B.java:
public interface B { String FooBar(String s); }
我想将它与继承 B 的 Python 类 D 一起使用,就像这样。 D.py:
class D(B):
def FooBar(s)
return s + 'e'
那么现在我如何在java中获取D的实例呢?很抱歉我问了这样一个 n00b 问题,但 Jython 文档很糟糕/部分离线。
上面示例的代码。您还需要更改 FooBar 实现以采用 self 参数,因为它不是静态方法。
您需要在类路径中包含 jython.jar 才能编译和运行此示例。
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class Main {
public static B create()
{
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("from D import D");
PyObject DClass = interpreter.get("D");
PyObject DObject = DClass.__call__();
return (B)DObject.__tojava__(B.class);
}
public static void main(String[] args)
{
B b = create();
System.out.println(b.FooBar("Wall-"));
}
}
有关详细信息,请参阅以下章节Jython 和 Java 集成 http://jythonpodcast.hostjava.net/jythonbook/chapter10.html in the Jython 书 http://jythonpodcast.hostjava.net/jythonbook/index.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)