是的,这可完成。通常这将通过创建一个PythonInterpreter
对象,然后使用它来调用python类来完成。
请看下面的例子:
的Java:
import org.python.core.PyInstance;
import org.python.util.PythonInterpreter;
public class InterpreterExample
{
PythonInterpreter interpreter = null;
public InterpreterExample()
{
PythonInterpreter.initialize(System.getProperties(),
System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
}
void execfile(final String fileName)
{
this.interpreter.execfile(fileName);
}
PyInstance createClass(final String className, final String opts)
{
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
public static void main(String gargs[])
{
InterpreterExample ie = new InterpreterExample();
ie.execfile("hello.py");
PyInstance hello = ie.createClass("Hello", "None");
hello.invoke("run");
}
}
的Python:
class Hello:
__gui = None
def __init__(self, gui):
self.__gui = gui
def run(self):
print 'Hello world!'
不以同样的方式没有了,更重要的是有没有理由。 – Serdalis 2013-05-09 11:27:30
答案是否适合您的需求?如果他们解决了您的问题,请选择一个答案,这样问题就不会被标记为未答复。谢谢 – Dropout 2013-05-10 07:19:13