<dependency> <groupId>org.python</groupId> <artifactId>jython-standalone</artifactId> <version>2.7.2</version> </dependency> python class Calculator: def add(self, a, b): return a + b def multiply(self, a, b): return a * b import org.python.core.Py; import org.python.core.PyObject; import org.python.util.PythonInterpreter; public class JythonExample { public static void main(String[] args) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("from script import Calculator"); PyObject pyObject = interpreter.eval("Calculator()"); PyObject result = pyObject.invoke("add", Py.newInteger(2), Py.newInteger(3)); System.out.println(result); result = pyObject.invoke("multiply", Py.newInteger(2), Py.newInteger(3)); System.out.println(result); } }


上一篇:
下一篇:
切换中文