Beanshell翻译4

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

Beanshell翻译4

http://user.qzone.qq.com/281696143/blog/1216273325  Ronger
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: 这儿是一些示例:
/*
Standard Java syntax 标准Java语法
*/
// 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将在运行是进行类型检查。
所以,我们可以去掉上例中左边的类型并且可以写成下面这样:
/*
Loosely Typed Java syntax 松散定义Java类型
*/
// 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'是马上实现,其它的当前是忽略的。修改不可以应用在没有类型的变量上。


相关阅读:

TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-05-03  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

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

RSS订阅

Open Toolbar