天天看點

Beanshell翻譯8

 1.scrīpting Interfaces 腳本接口

One of the most powerful features of BeanShell is the ability to scrīpt Java interfaces.

This feature allows you to write scrīpts that serve as event handlers, listeners,

and components of other Java APIs.

It also makes calling scrīpted components from within your applications easier

because they can be made to look just like any other Java object.

在Beanshll中最強大的特性是腳本支援接口的能力。

這個特性允許你寫可以為時間處理,監聽器,其它的Java元件服務的腳本。

它還可以讓你從你的應用程式中調用腳本元件變得容易,

因為他們看起來和其它的Java對象一樣。

2.Anonymous Inner.Class Style 匿名的内部類樣式

One way to get a scrīpted component to implement a Java interface

is by using the standard Java anonymous inner class syntax

to construct a scrīpted object implementing the interface type. For example:

取得一個實作了Java接口的腳本元件的方法,

是使用标準的Java匿名類建立一個實作接口的腳本對象。例如:

buttonHandler = new ActionListener() {

actionPerformed( event ) {

print(event);

}

};

button = new JButton();

button.addActionListener( buttonHandler );

frame(button);

In the above example we have created an object that implements the ActionListener interface

and assigned it to a variable called buttonHandler.

The buttonHandler object contains the scrīpted method actionPerformed(),

which will be called to handle invocations of that method on the interface.

在上面的例子中,我們建立一個實作ActionListener接口的對象,并且定義它為buttonHandler.

buttonHandler對象包含腳本方法actionPerformed(),

這個方法在處理事件的時候會被調用。

Note that in the example we registered our scrīpted ActionListener

with a JButton using its addActionListener() method.

The JButton is, of course, a standard Swing component written in Java.

It has no knowledge that when it invokes the buttonHandler's actionPerformed() method

it will actually be causing the BeanShell interpreter to run a scrīpt to evaluate the outcome.

現在在這個例子中,我們通過addActionListener()方法給按鈕注冊腳本監聽器。

這個按鈕,當然應該是,用Java寫成的一個标準的Swing元件。

它還不知道,當它調用buttonHandler的actionPerformed()方法時,

它将完全的依靠Beanshll解釋器運作腳本得到結果。

To generalize beyond this example a bit .

scrīpted interfaces work by looking for scrīpted methods to implement the methods of the interface.

A Java method invocation on a scrīpt that implements an interface

causes BeanShell to look for a corresponding scrīpted method with a matching signature

(name and argument types). BeanShell then invokes the method,

passing along the arguments and passing back any return value.

When BeanShell runs in the same Java VM as the rest of the code,

you can freely pass "live" Java objects as arguments and return values,

working with them dynamically in your scrīpts; the integration can be seamless.

See also the dragText example.

超出這個例子進行概括。腳本接口通過尋找實作接口的函數來工作。

一個實作接口的腳本函數,使Beanshell尋找一個相應的腳本函數(通過名字和參數類型比對)。

Beanshell于是可以調用這個方法,通過參數,得到傳回值。

當Beanshell和其它的代碼一起在同一個Java虛拟機中工作時,你可以自由的使用存活的

Java對象作為參數和傳回值,在你的腳本中和他們一起動态的工作。

也可以看看dratText這個例子。

3.'this' references as Interface Types 用在接口類型中的'this'引用

The anonymous inner class style syntax which

we just discussed allows you to explicitly create an object of a specified interface type,

just as you would in Java. But BeanShell is more flexible than that.

In fact, within your BeanShell scrīpts,

any 'this' type scrīpt reference can automatically implement any interface type, as needed.

This means that you can simply use a 'this' reference to your scrīpt or a scrīpted object anywhere that

you would use the interface type.

BeanShell will automatically "cast" it to the correct type

and perform the method delegation for you.

匿名的内部類文法,我們可以允許你顯式的定義一個指定接口類型的對象,就像在Java中一樣。

但是Beanshell可以比這個更加靈活。

實際上,通過你的Beanshell腳本,需要的時候,

一些'this'類型的腳本引用可以動态的實作一些接口類型。

這些的意思就是,你可以簡單的在你的腳本中使用一個'this'引用,

或者簡單的使用'this'引用指向一個腳本對象,

這個對象是可以使用接口類型的用在任何地方的對象。

Beanshell将自動的将它轉換成正确的類型并且執行相應的函數。

For example, we could scrīpt an event handler for our button even more simply using just a global method,

like this:

例如,通過很簡單的使用一個全局方法,我們可以為我們的按鈕定義一個事件處理,像下面這樣:

actionPerformed( event ) {

print( event );

}

button = new JButton("Foo!");

button.addActionListener( this );

frame( button );

Here, instead of making a scrīpted object to hold our actionPerformed() method

we have simply placed the method in the current context (the global scope)

and told BeanShell to look there for the method.

在這兒,我們不是使用一個腳本對象儲存actionPerformed()方法,

我們是簡單的将這個方法放在目前的環境中(全局作用域),并且告訴Beanshell去哪兒尋找這個方法。

Just as before, when ActionEventsare fired by the button,

your actionPerformed() method will be invoked.

The BeanShell 'this' reference to our scrīpt implements the interface

and directs method invocations to the appropriately named method, if it exists.

就像以前一樣,當ActionEventsare被按鈕激發,你的actionPerformed()方法将被調用。

Beanshell中腳本的'this'引用實作了接口,并且引導程式找到存在的适合的方法。

Note: 注意:

If you want to have some fun,

try entering the previous example interactively in a shell or on the command line.

You'll see that you can then redefine actionPerformed() as often as you like

by simply entering the method again.

Each button press will find the current version in your shell. In a sense,

you are working inside a dynamic Java object that you are creating and modifying as you type.

Neat, huh? Be the Bean! Of course,

you don't have to define all of your interface methods globally.

You can create references in any scope, as we discussed in "scrīpting Objects".

For example, the following code creates a scrīpted message button object

which displays a message when its pushed.

The scrīpted object holds its own actionPerformed() method,

along with a variable to hold the Frame used for the GUI:

如果你想有趣一些,嘗試将上面的例子中的代碼順序的輸入在shell或者指令行上。

你可以看到,你可以以你喜歡的方式通過簡單的再次輸入函數來重新定義actionPerformed()函數。

每一個按鈕的點選都會在你的shell中尋找到目前的版本。

感覺上,你在一個自己建立和定義的動态Java對象内部工作。

當然,在全局作用域中,你不用定義接口中的所有方法。

你可以在一些作用域中建立引用,就像我們在"腳本對象"中讨論的一樣。

例如,下面的代碼建立了一個腳本消息按鈕對象,當這個按鈕被點選的時候會顯示消息。

這個腳本對象儲存它自己的actionPerformed()方法,同時通過一個變量儲存用來GUI中的Frame.

messageButton( message ) {

JButton button = new JButton("Press Me");

button.addActionListener( this );

JFrame frame = frame( button );

actionPerformed( e ) {

print( message );

frame.setVisible(false);

}

}

messageButton("Hey you!");

messageButton("Another message...");

The above example creates two buttons, with separate messages.

Each button prints its message when pushed and then dismisses itself.

The buttons are created by separate calls to the messageButton() method,

so each will have its own method context, separate local variables,

and a separate instance of the ActionListener interface handler.

Each registers itself (its own method context) as the ActionListener for its button,

using its own 'this' reference.

上面的例子中建立了兩個按鈕,它們會彈出不同的資訊。

當點選按鈕并且釋放它,每個按鈕都會列印它的資訊。

這兩個按鈕是分别被messageButton()方法建立的,是以每一個都有它自己的方法環境,

不同的本地變量,和不同的ActionListener接口處理執行個體。

每一個都為了它的按鈕當做ActionListener一樣來注冊自己,使用它自己的'this'引用。

In this example all of the "action" is contained in messageButton() method context.

It serves as a scrīpted object that implements the interface and also holds some state,

the frame variable, which is used to dismiss the GUI.

More generally however, as we saw in the "scrīpting Objects" section,

we could have returned the 'this' reference to the caller,

allowing it to work with our messageButton object in other ways.

在這個例子中,所有的"action"都被包含在messageButton()方法環境中。

它為實作接口和儲存狀态的腳本對象服務,frame變量,用來讓GUI畫面消失。

更多的,就像我們在"腳本對象"章節中看到在,

我們可以傳回'this'引用給調用者,允許它和我們的messageButton對象以其它的方式一起工作。