天天看點

Beanshell翻譯4

 1.Basic Syntax 基本文法

BeanShell is, foremost, a Java interpreter.

So you probably already know most of what you need to start scrīpting with BeanShell.

This section describes specifically what portion of the Java language BeanShell

interprets and how BeanShell extends it or "loosens" it to be more scrīpting language like.

Beanshell更多的,是一個Java解釋者。于是你已經大概知道了開始腳本語言Beanshell時你需要什麼。

這一部分描述Beanshell解釋者的一部分和Beanshell是怎樣比普通的腳本語言更好的擴充它。

2.Standard Java Syntax 标準Java文法

In a BeanShell scrīpt (and on the command line) you can type normal Java statements and expressions and

display the results.

Statements and expressions are the kinds of things you normally find inside of a Java

method: variable assignments, method calls, math expressions, for.loops, etc.

在一個Beanshell的腳本中你可以定義普通的Java申明和表達式并且可以顯示結果。

申明和表達式一類你通常可以在如下的地方可以看到的:

Java方法,變量申明,方法調用,數學表達式,for循環等等。

Here are some examples: 這兒是一些示例:

// Use a hashtable 使用hashtable

Hashtable hashtable = new Hashtable();

Date date = new Date();

hashtable.put( "today", date );

// Print the current clock value 列印目前時間

print( System.currentTimeMillis() );

// Loop 循環

for (int i=0; i<5; i++)

print(i);

// Pop up a frame with a button in it 彈出一個帶有按鈕的面闆

JButton button = new JButton( "My Button" );

JFrame frame = new JFrame( "My Frame" );

frame.getContentPane().add( button, "Center" );

frame.pack();

frame.setVisible(true);

You can also define your own methods and use them just as you would inside a Java class.

We'll get to that in a moment.

你也可以就像在一個Java類中一樣定義你自己的方法并且使用它們。稍等我們将開始。

3.Loosely Typed Java Syntax 松散定義Java類型

In the examples above, all of our variables have declared types.e.g. "JButton button".

Beanshell will enforce these types,

as you will see if you later try to assign something other than a JButton to the variable "button"

(you will get an error message).

However BeanShell also supports "loose" or dynamically typed variables.

That is, you can refer to variables without declaring them first and without specifying any type.

In this case BeanShell will do type checking where appropriate at runtime.

So, for example, we could have left off the types in the above example and written all of the above as:

在上面的例子中,所有的變量都定義了類型。比如JButton button.

Beanshell将檢查這些類型,如果你嘗試将其它類型的變量指派給"button",你可以看到你将得到一個錯誤的消息。

但是Beanshell也支援“松散”或者動态定義變量。

那就是說,你可以使用沒有定義并且沒有申明類型的變量。

在這個案例中Beanshell将在運作是進行類型檢查。

是以,我們可以去掉上例中左邊的類型并且可以寫成下面這樣:

// Use a hashtable 使用hashtable

hashtable = new Hashtable();

hashtable.put( "today", date );

// Print the current clock value 列印目前時間

print( System.currentTimeMillis() );

// Loop 循環

for (i=0; i<5; i++)

print(i);

// Pop up a frame with a button in it  彈出一個帶有按鈕的面闆

button = new JButton( "My Button" );

frame = new JFrame( "My Frame" );

frame.getContentPane().add( button, "Center" );

frame.pack();

frame.setVisible(true);

This may not seem like it has saved us a great deal of work.

But you will see the difference when you come to

rely on scrīpting as part of your development and testing process;

especially for in interactive use.

When a "loose" variable is used you are free to reassign it to another type of Java object later.

Untyped BeanShell variables can also freely hold Java primitive values like int and boolean.

Don't worry, BeanShell always knows the real types and only lets you use the values where appropriate.

For primitive types this includes doing the correct numeric promotion

that the real Java language would do when you use them in an expression.

這些看起來不會給我們帶來很多的工作。但是當你将腳本當成你的應用程式的一部分的時候,你将看到不同,

特别是在互動的使用中。當一個松散類型使用的時候你可以自由的将它賦給另一個Java對象。

無類型Beanshell變量也可以支援Java原始類型int和boolean.

不要擔心,Beanshell總是知道真實的類型并且允許你在适當的時候使用變量的值。

對于原始類型來說,這些包含正确的數字校驗,當你在一個表達式中使用它們的時候,

正式的Java語言将會這樣做。

4.Exception Handling 異常處理

Exception handling using try/catch blocks works just as it does in Java. For example:

就像在Java語言中一樣,異常處理使用try/catch塊。舉例:

try {

int i = 1/0;

} catch ( ArithmeticException e ) {

print( e );

}

But you can loosely type your catch blocks if you wish:

如果你希望你也可以松散定義你的catch塊。

try {

...

} catch ( e ) {

print( "caught exception: "+e );

}

5.Basic Scoping of Variables 變量的有效作用空間

Note: 注意:

As of BeanShell version 1.3 the default scoping of loosely typed variables was changed to be

more consistent with Java.

BeanShell still supports an alternate scoping used in earlier versions.

This mode can be enabled for legacy code by setting the system property "localscoping" to true.

See appendix "Local Scoping".

在Beanshell 1.3版本中松散類型變量的預設作用空間變化的和Java中一緻。

Beanshell仍然支援用在早期版本中的交替作用空間。通過設定系統屬性"localscoping"為true

為了支援遺留代碼這種模式也可以啟用。見附錄“本地作用空間”。

Exception Handling 異常處理

Variable scoping in BeanShell behaves, wherever possible, just like that in Java.

Ordinary Java, however,

does not offer "loose" variables (variables that can be used without being declared first).

So we must define their behavīor within BeanShell.

We'll see in the next section that untyped variables .

variables that are not declared and not assigned a value elsewhere .

default to the local scope. This means that, in general,

if you assign a value to a variable without first declaring it, you are creating a new local variable in the current scope.

變量作用空間在Beanshell中的行為表現,就像在Java中一樣。

普通的Java,沒有提供“松散類型”變量(開始沒有定義就可以使用的變量)。

當然我們必須在Beanshell中定義它們的行為。我們将在下面的篇幅中看到沒有類型的變量。

沒有定義并且沒有指派的變量,預設是本地變量。這個意思就是說,通常情況下,

如果你開始沒有定義一個變量,就将值賦給它,你就是在本地作用域中建立了一個本地變量。

6.Blocks 程式塊

Blocks are statements between curly braces {}.

In BeanShell, as in Java, blocks define a level of scope for typed variables:

typed variables declared within a block are local to the block.

Other assignments within the block occur, as always, wherever the variable was defined.

程式塊語句是在兩個符号{}之間的部分。在Beanshell中就和在Java中一樣,程式塊為定義的變量

聲明了一個作用域,不管變量定義在那裡,在程式塊中的其它任務和通常的一樣。

Untyped variables in BeanShell, however, are not constrained by blocks.

Instead they act as if they were declared at the outer (enclosing) scope's level.

With this in mind, BeanShell code looks just like Java code.

In BeanShell if you declare a typed variable within a block it is local to the block.

But if you use an untyped variable

(which looks just like an ordinary assignment in Java)

it behaves as an assignment to the enclosing scope.

在Beanshell中沒有類型的變量,沒有被程式塊制約。它們的行為就和定義在外面的變量一樣。

考慮到這些,Beanshll代碼看起來很像Java代碼。在Beanshell中如果你在一個程式塊中定義了一個

有類型的變量,它就是程式塊中的本地變量。

但是如果你使用一個沒有類型的變量(看起來是一個Java中普通的作業)

,它的行為就像在一個封閉作用域中的任務一樣。

This will make sense with a few examples: 通過下面的例子來找一下感覺。

// Arbitrary code block 任意的代碼塊

{

y = 2; // Untyped variable assigned 沒有類型定義的變量

int x = 1; // Typed variable assigned 有定義類型的變量

}

print( y ); // 2

print( x ); // Error! x is undefined. 錯誤!X的類型不明确。

// Same with any block statement: if, while, try/catch, etc.

和這些語句塊一樣:if,while,try/catch,等等。

if ( true ) {

y = 2; // Untyped variable assigned 沒有類型定義的變量

int x = 1; // Typed variable assigned 有定義類型的變量

}

print( y ); // 2

print( x ); // Error! x is undefined. 錯誤!X的類型不明确。

Variables declared in the for.init area of a for.loop follow the same rules as part of the block:

定義在for循環中的for初始化中的變量同程式塊中的其它部分一樣遵守同樣的規則。

for( int i=0; i<10; i++ ) { // typed for.init variable有類型的for.init中的變量

j=42;

}

print( i ); // Error! 'i' is undefined. 錯誤!i的類型不明确。

print( j ); // 42

for( z=0; z<10; z++ ) { } // untyped for.init variable沒有類型的for.init中的變量

print( z ); // 10  

7.Variable Modifiers 變量修改

The standard Java variable modifiers may be used on typed variables:

private / protected / public, final,

transient, volatile, static. Only 'final' is currently implemented. The others are currently ignored.

Modifiers may not be applied to untyped variables.

标準的Java變量的修改可以用在類型變量中:

private/protected/public,final,transient,volatile,static.

僅僅'final'是馬上實作,其它的目前是忽略的。修改不可以應用在沒有類型的變量上。

下一篇: Beanshell翻譯2