是的,可以做到。通常,这将通过创建一个
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!'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)