天天看點

Beanshell翻譯5

 1.In BeanShell you may access JavaBean properties as if they were fields:

在Beanshell中你可以通路JavaBean中的屬性:

button = new java.awt.Button();

button.label = "my button"; // Equivalent to等價于: b.setLabel("my button");

print( button.label ); // Equivalent to 等價于 print( b.getLabel() ); 等價于

JavaBean properties are simply pairs of "setter" and "getter" methods

that adhere to a naming convention.

In the above example BeanShell located a "setter" method with the name "setLabel()"

and used it to assign the string value.

It then found the method named getLabel() to retrieve the value.

JavaBean的屬性是一類簡單的"setter"和"getter"方法。在上面的例子中,Beanshell通過名字

"setLabel()"來定為一個"setter"方法,并且通過它來指派,通過getLable()方法取值。

Boolean properties may optionally use the syntax "is" for their "getter". e.g.

Boolean屬性可以使用"is"來代替"getter"

Float f = new Float(42f);

print( f.infinite ); // Equivalent to 等價于 print( f.isInfinite() ); // false

If there is any ambiguity with an actual Java field name of the object

(e.g. label in the above example) then the

actual field name takes precedence.

If you wish to avoid any ambiguity BeanShell provides an additional,

uniform syntax for accessing both Java Bean properties and Hashtable or Map entries.

You may use the "{}" curly brace construct

with a String identifier as a qualifier on any variable of the appropriate type:

如果和實際的對象的屬性名字有不明确的地方(比如上面的例子中的label),

将優先考慮實際的屬性名稱。如果你希望避免Beanshell中不明确的地方。

Hashtable,Map和JavaBean屬性都可以用使用。

你可以使用"{}"構造一個字元串來限定适當類型的變量。

b = new java.awt.Button();

b{"label"} = "my button"; // Equivalent to 等價于: b.setLabel("my button");

h = new Hashtable();

h{"foo"} = "bar"; // Equivalent to等價于: h.put("foo", "bar");

Where the java.util.Collections API is available, Maps are also supported.

java.util.Collection已經導入,Maps也是可以支援的。

2.Enhanced 'for' Loop 支援循環

BeanShell supports the Java 1.5 style enhanced for.loop for iterating over collections and array types.

(Note that you do not have to be running Java 1.5 to use this feature).

Beanshell支援Java1.5以上的for.loop功能,用來周遊集合和數組。

(注意你不能通過運作Java1.5來使用這個特性)

List foo = getSomeList();

for ( untypedElement : foo )

print( untypedElement );

for ( Object typedElement: foo )

print( typedElement );

int [] array = new int [] { 1, 2, 3 };

for( i : array )

print(i);

for( char c : "a string" )

print( c );

Supported iterable types include all the obvious things. 支援包含下面這些集合類型

· JDK 1.1+ . (no collections): Enumeration, arrays, Vector, String, StringBuffer

Convenience Syntax 便利的文法

· JDK 1.2+ . (w/collections): Collections, Iterator

See also the BshIterator API which supports the ehanced for.loop and allows iteration over these types using

the dynamically loaded BeanShell Collection manager.

也可以看到BshIterator API,支援for.loop并且通過使用動态加載Beanshell集合管理器來允許周遊更多的類型。

3.Switch Statements 選擇語句

In BeanShell, the switch statement may be used not only with numeric types but with objects.

For example,

you may switch on Dates and Strings which are compared for equality with their equals() methods:

在Beanshell中,選擇語句不僅可以用在數字類型中,也可以用于對象。

比如,你可以通過equals()方法來比較Dates和Strings類型。

dateobj = new Date();

switch( dateobj )

{

case newYears:

break;

case christmas:

break;

default:

}

4.Auto Boxing and Unboxing 自動包裝和拆包

"Boxing" and "Unboxing" are the terms used to describe automatically wrapping a primitive type in a wrapper

class and unwrapping it as necessary.

Boxing is a feature of Java (SDK1.5) and has been supported in

BeanShell for many years.

BeanShell supports boxing and unboxing of primitive types. For example:

"包裝"和"拆包"是一種技術,當需要的時候,這種技術是用來自動包裝基礎類型和拆包的。

包裝是一個Java1.5的特性并且Beanshell很多年前就可以支援這些。Beanshell支援

基礎類型的包裝和拆包。例如:

int i=5;

Integer iw = new Integer(5);

print( i * iw ); // 25

Vector v = new Vector();

v.put(1);

int x = v.getFirstElement();

Importing Classes and Packages 導入類和包

In BeanShell as in Java, you can either refer to classes by their fully qualified names,

or you can import one or more classes from a Java package.

在Beanshll中就像和在Java中一樣,你可以通過類的全名來導入一個類,

或者你可以從一個包中導入一個或多個類。

// Standard Java 标準Java

import javax.xml.parsers.*;

import mypackage.MyClass;

In BeanShell import statements may appear anywhere, even inside a method,

not just at the top of a file.

In the event of a conflict, later imports take precedence over earlier ones.

A somewhat experimental feature is the "super import".

With it you may automatically import the entire classpath, like so:

在Beanshll中導入一個申明可以用在任何地方,甚至在一個方法裡面,

而不僅僅是在一個檔案的頂部。在沖突的時候,後導入的優先于前面導入的。

一些新的特性是"super import"。通過它你可以自動的導入完整的類路徑,像下面一樣:

5.Switch Statements 選擇申明

import *; import *;

The first time you do this BeanShell will map out your entire classpath;

so this is primarily intended for interactive use.

Note that importing every class in your classpath can be time consuming.

It can also result in a lot of ambiguities.

Currently BeanShell will report an error when resolving an ambiguous import from

mapping the entire classpath.

You may disambiguate it by importing the class you intend.

第一次你做這些,Beanshell将覆寫完整的類路徑,是以這是主要的互動使用。

注意在通過你的類路徑來導入會很消耗時間。它也會導入不明确的類。

當通過導入類路徑而産生了一個不明确的導入時,Beanshell将産生一個錯誤。

你可以通過導入你想要的類來明确它。

Tip: 提示:

The BeanShell which() command will use the classpath mapping capability to tell you

where exactly in your classpath a specified class is located:

Beanshell的which()指令将在類路徑中尋找一個确切的類型的完整路徑:

bsh % which( java.lang.String );

Jar: file:/usr/java/j2sdk1.4.0/jre/lib/rt.jar

See "Class Path Management" for information about modifying the BeanShell classpath at run.

time with the addClassPath() or setClassPath() commands.

Also see "BeanShell Commands" for information about importing new BeanShell commands from the

classpath.

通過"Class Path Management"獲得更多資訊,在運作時修改Beanshell類路徑,

通過addClassPath()或者setClassPath()指令。

也可以通過"Beanshell Commands"獲得更多資訊,從類路徑中導入新的Beanshell指令。

6.Default Imports 預設導入

By default, common Java core and extension packages are imported for you.

They are, in the order in which they are imported:

通過預設導入,通用的Java核心和擴充包被導入進來。他們是以下的這些:

· javax.swing.event

· javax.swing

· java.awt.event

· java.awt

· java.net

· java.util

· java.io

· java.lang

Two BeanShell package classes are also imported by default:

兩個Beanshll包中的類也被預設導入:

· bsh.EvalError

· bsh.Interpreter

Finally, we should mention that BeanShell commands may be imported from the classpath.

The default commands are imported in the following way:

最後,我們會說起,Beanshell的指令可以從類路徑中導入。預設的指令是從下面的路徑導入的:

importCommands("/bsh/commands");

We will discuss how to import your own commands in a later section.

我們将在後面的章節中讨論怎樣導入你自己的指令。

Tip: 提示:

The classes java.awt.List and java.util.List are both imported by default.

Because java.util.List is imported later, as part of the java.util package,

it takes precedence. To access java.awt.List simply import it in,

or the java.awt package again your scrīpt. Later imports take precedence.

類型java.awt.List和java.util.List都是通過預設導入的。因為java.util.List

(作為java.util包中的一部分)是後導入的,是以它有更高的優先級。