Beanshell翻译8

上一篇 / 下一篇  2008-09-02 20:19:05 / 个人分类:Beanshell

Beanshell翻译8

http://user.qzone.qq.com/281696143/blog/1217472349   Ronger
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对象以其它的方式一起工作。
 


相关阅读:

TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-03-20  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 25606
  • 日志数: 25
  • 建立时间: 2008-08-27
  • 更新时间: 2008-09-02

RSS订阅

Open Toolbar