Java调用python的方法(jython)
发布时间:2020-12-16 20:57:31 所属栏目:Python 来源:网络整理
导读:1 什么是jython? 他其实是一门语言,并非是Java 或者Python的解释器.用它可以实现,java和python代码的互相访问。 2 简单的例子 java中执行python 语句 PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("days=('mod','Tue','We
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 learningmachinelearninginactionCh02test.py"); PyFunction function = (PyFunction)interpreter.get("pluser",PyFunction.class); PyObject o = function.__call__(new PyInteger(8),new PyInteger(23)); 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |