【转】Jython简单入门

1. 用Jython调用Java类库 

第一步、创建Java类 

写一个简单的Java类,用Point来示例: 

Java代码  
  1. import org.python.core.*;  
  2.   
  3. public class Point extends PyObject  
  4. {  
  5.     private int x;  
  6.     private int y;  
  7.   
  8.     public Point()  
  9.     {  
  10.         x = 0;  
  11.         y = 0;  
  12.     }  
  13.   
  14.     public Point(int x, int y)  
  15.     {  
  16.         this.x = x;  
  17.         this.y = y;  
  18.     }  
  19.   
  20.     public void dump()  
  21.     {  
  22.         System.out.printf("The position is (%s, %s)\n", x , y);  
  23.     }  
  24. }  


编译的时候,记得把jython.jar加入类环境中: 

Shell代码  
  1. export CLASSPATH=/usr/share/java/jython.jar  
  2. javac Point.java  



第二步、简单调用 

现在可以编写Jython来调用上面的Java类库了 

Python代码  
  1. #!/usr/bin/env jython  
  2.   
  3. import Point  
  4.   
  5. if __name__ == "__main__":  
  6.     p = Point(2,3)  
  7.     p.dump()  



第三步、扩展 

Python代码  
  1. import Point  
  2.   
  3. class Circle(Point):  
  4.     def __init__(self, x, y, r):  
  5.         super(Circle, self).__init__(x, y)  
  6.         self.r = r;  
  7.   
  8.     def dump(self):  
  9.         super(Circle, self).dump()  
  10.         print "This radius of Circle is %s" % self.r  
  11.   
  12.     def area(self):  
  13.         return self.r**2*3.1415  
  14.   
  15. if __name__ == "__main__":  
  16.     p = Point(2,3)  
  17.     p.dump()  
  18.     c = Circle(2,10,3# 但是实际上这里有问题,我不知道怎么回事,还在研究中……哪位指点下  
  19.     c.dump()  


虽然测试的时候有问题,但是隐约感觉到了Jython的强大,让我们欢呼一下吧 

2. 用Java执行Python代码 

在安装好的Demo里有个例子,可以拿出来炫炫 

Java代码  
  1. import org.python.core.PyException;  
  2. import org.python.core.PyInteger;  
  3. import org.python.core.PyObject;  
  4. import org.python.util.PythonInterpreter;  
  5.   
  6. public class SimpleEmbedded {  
  7.   
  8.     public static void main(String[] args) throws PyException {  
  9.         PythonInterpreter interp = new PythonInterpreter();  
  10.         interp.exec("import sys");  
  11.         interp.exec("print sys");  
  12.         interp.set("a"new PyInteger(42));  
  13.         interp.exec("print a");  
  14.         interp.exec("x = 2 + 2");  
  15.         PyObject x = interp.get("x");  
  16.         System.out.println("x: " + x);  
  17.     }  
  18. }  


结果如下: 

Shell代码  
  1. $ javac SimpleEmbedded.java   
  2. $ java SimpleEmbedded  
  3. sys module  
  4. 42  
  5. x: 4  
  6. $  



3. 直接在Jython中使用Java内部的类库 

如果你不介意,当然可以在Jython中交互执行Java的类库。下面是一个有点“实用”的例子: 

Python代码  
  1. $ jython  
  2. Jython 2.2.1 on java1.6.0_0  
  3. Type "copyright""credits" or "license" for more information.  
  4. >>> import sys  
  5. >>> from java.net import URL  
  6. >>> url = URL("http://willzh.iteye.com")  
  7. >>> urlopen = url.openConnection()  
  8. >>> input = urlopen.getInputStream()  
  9. >>> c = input.read()  
  10. >>> while c!=-1:  
  11.     sys.stdout.write(chr(c))  
  12.     c = input.read()  



安装好的Demo里有些例子也可以参考,例如: 

Python代码  
  1. from java.applet import Applet  
  2. import sys  
  3.   
  4. class HelloWorld(Applet):  
  5.     def paint(self, g):  
  6.     g.drawString("Hello from Jython %s!" % sys.version, 2030)  
  7.   
  8.   
  9. if __name__ == '__main__':  
  10.     import pawt  
  11.     pawt.test(HelloWorld())  


直接调用就可以了: 

Shell代码  
  1. jython HelloJython.py # 假设上面代码的文件名是HelloJython.py  
  2. java org.python.util.jython HelloJython.py # 也可以这样调用  




4. 将Python代码编译为Java类 

第一步、创建Python类 Goo.py 

Python代码  
  1. import java.lang.Object  
  2.   
  3. class Goo(java.lang.Object):  
  4.     def __init__(self):  
  5.         '''''public Goo()'''  
  6.   
  7.     def dump(self):  
  8.         '''''@sig public void dump()'''  
  9.         print 'goooooo'  


注意函数文档中的字符串,public Goo()表示告诉Java这是构造函数。 

第二步、创建Java测试类 GooTest.java 

Java代码  
  1. public class GooTest {  
  2.     public static void main(String[] args)  
  3.     {  
  4.         Goo goo = new Goo();  
  5.         goo.dump();  
  6.     }  
  7. }  



第三步、编译 
终端下运行如下命令: 

Python代码  
  1. # jythonc Goo.py  
  2. # javac -cp jpywork:$CLASSPATH GooTest.java # 将jpywork路径加入搜索路径中  
  3. # java -cp jpywork:$CLASSPATH GooTest  
  4. goooooo  


运行成功! 

5. 后记 

很多人说,Python比Java开发速度来的要快,但是Java也有Java不可动摇的强大之处,如此结合,想必Jython的好处和作用显而易见了吧。 

本文转自:http://willzh.iteye.com/blog/307222

posted @ 2013-08-15 22:12  天涯雪  阅读(1180)  评论(0编辑  收藏  举报