1 / 47

Jython 学习 张波

Jython 学习 张波. Jython 简介 Jython 类型 Jython 面向对象与面向函数 Jython 运算符 Jython 条件与异常控制 与 Java 的关系和相互调用 Jython 线程 Jython 注解 Jython 集合操作 内省 Jython 开发环境搭建. Jython 简介.

lydie
Download Presentation

Jython 学习 张波

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Jython学习张波 Jython简介 Jython类型 Jython面向对象与面向函数 Jython运算符 Jython条件与异常控制 与Java的关系和相互调用 Jython线程 Jython注解 Jython集合操作 内省 Jython开发环境搭建

  2. Jython简介 • Jython是一种完整的语言,而不是一个Java翻译器或仅仅是一个Python编译器,它是一个Python语言在Java中的完全实现。 Jython也有很多从CPython中继承的模块库。最有趣的事情是Jython不像CPython或其他任何高级语言,它提供了对其实现语言的一切存 取。所以Jython不仅给你提供了Python的库,同时也提供了所有的Java类。这使其有一个巨大的资源库。

  3. Jython类型 • 数字值(int, float, long, complex) • 1, 1.0, 1L, 1+1J • Boolean型(bool) • Flase, True • 字符串(str) • “Oh my lady gaga, Jython!”, ‘Oh my lady gaga, Jython!’ • 列表(list) • [1, ‘abc’, 1j, 1.0, 1L] • 字典(dict) • {‘message’:’hello’} • 元组(tuple) • (1,2), (1,), () • 空(None) • None • 类型(type) • type(‘’) • 类(class) • class T:pass; type(T) # classobj • 实例(instance) • t=T(); type(t) #instance • 函数(function) • def test():pass; type(test) #function • 成员方法(instancemethod) • class T: def test(self):pass; type(T.test) # instancemethod • 注意: Flase=[], {}, (), ‘’, 0, 0.0, 0L, 0J, None

  4. Jython类型(Code) classobj:类 instancemethod:成员方法 instance:实例 function:函数 systemstate:系统状态 javapackage:Java包 module:模块

  5. Jython面向对象与面向函数 • 类(支持多重继承) • 方法 • 构造方法( __init__(self,…) ) • 成员方法(默认参数、不定参数、键值参数) • 变量 • 成员变量 • 类变量 • 局部变量 • 函数(默认参数、不定参数、键值参数) 注意: 继承多个Jython类,但只能直接或间接继承一个Java类。子类的 __init__ 方法必须显示调用其父类定义的所有 __init__ 方法

  6. Jython语法(Code) doc写在类,方法下面 默认参数 成员变量 覆盖str方法,在str()时执行

  7. Jython语法(Code) 不定参数 *v 键值参数 **v 直接访问对象属性 调用不定参数方法 键值参数调用

  8. Jython运算符 优先级(由高到低) • 四则运算符: • + - * / % ** += -= *= /= %=**= • 位运算符 • ~ | & ^ >> << |= &= >>= <<= ^= • 逻辑运算符 • and or not is in • 赋值运算符 • =

  9. Jython运算符(Code) 整除 幂 字符串运算 id(): 取对象id Id相同 元组是不允许改变内容的,Id不同 格式化字符串

  10. Jython条件与异常控制 • ifelse elif for while break continue try finally • except 捕捉异常 • raise 抛出异常 • pass 空语句

  11. Jython条件与异常控制(Code) for …. else, break不会执行else

  12. Jython条件与异常控制(Code) • Jython异常不是java.lang.Throwable的子类 运行结果:

  13. 与Java的关系和相互调用 • Jython在JVM执行 • Jython是在JVM中执行的解释型语言 • 与Java相互调用 • 常用类型的对应关系

  14. 与Java的关系和相互调用(Code) • 与Java相互调用 Java中调用Jython Jython中调用Java

  15. 与Java的关系和相互调用(Code) • 常用类型的对应关系

  16. Jython线程 • 线程的创建 • 使用Java中的线程 • Jython中的线程 • 同步 • Jython中同步对象 • Jython中同步方法 • Jython线程的控制 • Jython中threading模块

  17. Jython线程(Code) • 线程的创建 • 使用Java中的线程

  18. Jython线程(Code) • 线程的创建 • 使用Jython中的线程

  19. Jython线程(Code) • 同步 • 同步对象 同步stdout对象

  20. Jython线程(Code) • 同步 • 同步方法 执行结果:

  21. Jython线程(Code) • 关于同步方法的同步对象 执行结果:

  22. Jython线程(Code) • 线程控制 • thread.exit(), sys.exit() • 两者都有结束线程的作用, • 如果在是子线程中执行则只退出子线程 • 如果是在主线程里执行,会导致整个应用程序退出 • 注:这里使用的是thread模块 • 使用threading模块可以避免这个问题 • 应当少使用thread模块

  23. Jython中threading模块

  24. Jython中threading模块(Code) 执行结果:

  25. Jython注解 • 注解定义 • 注解使用 • AOP

  26. Jython注解(Code) remoteInvoke(invokeUMS)(url,xml) 执行结果:

  27. Jython集合操作 del不产生新的对象,其它的操作均返回新的对象,原数据不变 对于元组(tuple)不能使用del操作,其它同上

  28. Jython集合操作

  29. 内省

  30. 内省 注意:   只要全局变量没有被局部地重新绑定,那么在没有将其声明为全局的情况下,就可读取该变量。因此,只有在对全局变量赋值时才需要 global 语句。

  31. Jython开发环境搭建 • Eclipse + PyDev + Jython2.5.1 + JDK1.6

  32. 附录 字符串方法 列表方法 映射方法 内置函数 Jython库汇总 格式化字符串

  33. 字符串方法

  34. 字符串方法

  35. 列表方法

  36. 映射方法

  37. 内置函数

  38. 内置函数

  39. 内置函数

  40. 内置函数

  41. Jython库汇总

  42. Jython库汇总

  43. Jython库汇总

  44. Jython库汇总

  45. Jython库汇总

  46. 格式化字符串

  47. 参考 • Python核心编程(第二版) • Jython程序设计 • http://www.ibm.com/developerworks/cn/education/java/j-jython2/section2.html

More Related