是的,這可完成。通常這將通過創建一個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