Java库在Jython代码中的使用比在Java中稍微简单点。那是因为Jython自动处理了Java所隐含的一些编码过程。例如,Java GUI库的回调处理器可以是简单的Python函数,而Java编码人员需要在接口完全限定的类中提供方法(Java没有第一类的函数对象)。 Jython使Java类数据成员的存取既可以通过Python属性名(object.name),也可以通过对象构造器关键字参数(name=value);这种Python语法会转换成Java类的getName和setName存取函数的调用。在下面的例子中,我们会看到这些自动化窍门的实际运用。你并非必须使用这些窍门(Java程序员初次用这些技巧可能会搞糊涂),但是它们进一步简化了Jython编码,并使 Java类库更具Python的风味。
The net effect of all this is that Jython allows us to write Python programs that can run on any Java-aware machinein particular, in the context of most web browsers. More important, because Python programs are translated into Java bytecode, Jython provides an incredibly seamless and natural integration between the two languages. Both walk and talk in terms of the Java model, so calls across language boundaries are trivial. With Jython's approach, it's even possible to subclass a Java class in Python, and vice versa.
所有这些的实际效果是,Jython让我们写的Python程序可以运行在任何兼容Java的机器上,特别的,如大多数浏览器环境。更重要的,因为 Python程序能转化为Java字节码,Jython在两种语言之间不可置信地提供了一种无缝自然的集成。Jython以Java的模式走路与说话,因此跨语言边界调用变得简单。通过Jython,甚至可以在Python中继承Java类,或反过来。
18.4.2. Why Jython?
18.4.2. 为什么要Jython?
So why go to all this trouble to mix Python into Java environments? The most obvious answer is that Jython makes Java components easier to use: Jython scripts are typically a fraction of the size of their Java equivalents, and are much less complex. More generally, the answer is really the same as it is for C and C++ environments. Python, as an easy-to-use, object-oriented scripting language, naturally complements the Java programming language.
但是为什么要费劲地将Python掺入到Java环境中去呢?最明显的原因是Jython使Java部件更容易使用:Jython脚本比相同功能的 Java代码要简短的多,也没有那么复杂。而更一般的原因与C和C++环境下要集成Python的原因一致。Python作为一种简单易用,面向对象的脚本语言,很自然地补充了Java语言的不足。
By now, it is clear to most people that Java is too complex to serve as a scripting or rapid-development tool. But this is exactly where Python excels; by adding Python to the mix with Jython, we add a scripting component to Java systems, exactly as we do when integrating Python with C or C++. For instance, we can use Jython to quickly prototype Java systems, test Java classes interactively, and open up Java systems for end-user customization. In general, adding Python to Java development can significantly boost programmer productivity, just as it does for C and C++ systems.
到如今,大多数人都清楚,Java太复杂,不能用作脚本,或是快速开发工具。而这正是Python胜出的地方;Jython通过集成Python,给 Java系统添加了一个脚本部件,就像C/C++中集成Python一样。例如,我们可以用Jython快速创建Java系统的原型,交互式测试Java 类,或开放Java系统供用户定制。一般而言,Java加入Python可以显著地提高程序员的生产力,就像Python对C和C++系统的作用一样。
Jython Versus the Python C APIJython与Python C API对比Functionally, Jython is primarily an integration system: it allows us to mix Python with Java components. We also study ways to integrate Python with C and C++ components in a later part of this book. It's worth noting that we need different techniques to integrate Python with Java (such as the Jython compiler), because Java is a somewhat closed system: it prefers an all-Java mix. The C and C++ integration tools are generally less restrictive in terms of language assumptions, and any C-compatible language components will do. Java's strictness is partly due to its security goals, but the net effect is to foster integration techniques that are specific to Java alone. 功能上,Jython主要是一个集成系统:它允放我们混合Python和Java部件。本书后面我们还会研究Python集成C和C++部件的方法。值得注意的是,我们需要不同的技术来集成Python和Java(譬如Jython编译器),因为Java是一个有点封闭的系统:它更喜欢全Java的混合。 C和C++的集成工具在语言的假定上通常不太有限制性,可以使用任何C兼容的语言部件。Java的严格性在一定程度上缘于它的安全性目标,而结果就造成了 Java特有的集成技术。 On the other hand, because Java exposes runtime type information through its reflection API, Jython can largely automate the conversions and dispatching needed to access Java components from Python scripts; Python code simply imports and calls Java components. When mixing Python with C or C++, we must provide a "glue" code layer that integrates the two languages explicitly. Some of this can be automated (with the SWIG system we'll meet later in this text). No glue code is required in Jython, however, because Jython's (and Java's) developers have done all the linkage work already, in a generic fashion. It is also possible to mix in C/C++ components with Java via its native call interface (JNI), but this can be cumbersome and may cancel out Java's reported portability and security benefits. 另一方面,因为Java通过反射API暴露运行时类型信息,Jython可以在很大程度上自动化处理,从Python脚本操作Java部件时的转换与分派工作:Python代码只需简单地导入并调用Java部件。当混合Python与C或C++时,我们必须明确地提供一个“粘合”代码层来融合两种语言。其中部份工作可以自动化(如使用本书后面所述的SWIG系统)。而Jython不需要粘合代码,因为Jython(和Java)开发者已经以一种通用的方式,完成了链接工作。通过Java的本地调用接口(JNI),Java也是有可能混合C/C++部件的,但是这很麻烦,而且会消除Java所报导的可移植性和安全性。 |

