1 什么是jython?
他其实是一门语言,并非是java 或者python的解释器.用它可以实现,java和python代码的互相访问。
2 简单的例子
java中执行python 语句
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days;");java调用python的脚本:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("script.py"); java调用python类当中的函数
先在python文件中定一个python函数
def pluser(a,b):
# print "the result of pluser is %d" % (a+b)
return a+b
在java当中去调用:
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("F:\\machine learning\\machinelearninginaction\\Ch02\\test.py");
PyFunction function = (PyFunction)interpreter.get("pluser",PyFunction.class);
PyObject o = function.__call__(new PyInteger(8),new PyInteger(23));
本文介绍了Jython的基本概念,它是一种能够使Java和Python代码互相访问的语言。通过示例展示了如何在Java中执行Python语句、调用Python脚本以及调用Python类中的函数。
2181

被折叠的 条评论
为什么被折叠?



