1 什么是jython?
他其實(shí)是一門語(yǔ)言,并非是Java 或者Python的解釋器.用它可以實(shí)現(xiàn),java和python代碼的互相訪問(wèn)。
2 簡(jiǎn)單的例子
java中執(zhí)行python 語(yǔ)句
1
2
3
|
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec( "days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); " ); interpreter.exec( "print days;" ); |
java調(diào)用python的腳本:
1
2
|
PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile( "script.py" ); |
java調(diào)用python類當(dāng)中的函數(shù)
先在python文件中定一個(gè)python函數(shù)
1
2
3
|
def pluser(a,b): # print "the result of pluser is %d" % (a+b) return a + b |
在java當(dāng)中去調(diào)用:
1
2
3
4
|
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 )); |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/rually/article/details/51320477