jpython的使用详情

jpython

<dependency>
    <groupId>org.python</groupId>
    <artifactId>jython-standalone</artifactId>
    <version>2.7.0</version>
</dependency>

JAVA代码:

    public static void main(String[] args) {
            PythonInterpreter interpreter = new PythonInterpreter();  
            interpreter.execfile("C:\\Users\\Dick\\Desktop\\demo.py");  
            PyFunction func = (PyFunction)interpreter.get("wdd",PyFunction.class);  
            Integer a = 10;
            Integer b = 10;  
            PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));  
            System.out.println("anwser = " + pyobj.toString());  
    }

python脚本:

#open files  

print 'hello'  
number=[3,5,2,0,6]  
print number  
number.sort()  
print number  
number.append(0)  
print number  
print number.count(0)  
print number.index(5)  


def wdd(a, b):
    print(a+b)
    return a+b

print(wdd(1, 2))

def yyh(c, d):
    return c+d

print(yyh(5, 8))

运行结果:

hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3
3
3
13
20
anwser = 20

jar包下载链接:

http://www.jython.org/downloads.html    2.7.0

猜你喜欢

转载自blog.csdn.net/w1403575609/article/details/81284847
今日推荐