Beanshell翻译6

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

Beanshell翻译6

http://user.qzone.qq.com/281696143/blog/1217311291  Ronger
1.Document Friendly Entities 友好的文档
BeanShell supports special overloaded text forms of all common operators to make it easier to embed
BeanShell scrīpts inside other kinds of documents (e.g XML).
Beanshell支持文档类型,所有普通操作者容易嵌入的文档类型。
Beanshell脚本也可以放在其它的文档里面(比如XML).
scrīpted Methods 脚本函数
You can define methods in BeanShell, just as they would appear in Java:
你可以像在Java中一样,在Beanshell中定义函数:
int addTwoNumbers( int a, int b ) {
return a + b;
}
And you can use them in your scrīpts just as you would any Java method or "built.in" BeanShell command:
并且你可以在你的脚本中使用他们,就像你使用Java中的方法一样
sum = addTwoNumbers( 5, 7 );
Just as BeanShell variables may be dynamically typed,
methods may have dynamic argument and return types.
We could, for example, have declared our add() method above like so:
就像Beanshell变量可以动态的定义类型一样,方法也可以有动态类型的参数和返回值。
我们可以,像这样的定义上面的方法add():
add( a, b ) {
return a + b;
}
In this case, BeanShell would dynamically determine the types when the method is called and attempt to "do
the right thing":
在这个例子中,当这个函数被调用的时候,Beanshell可以动态的决定类型并且尝试执行正确的程序。
foo = add(1, 2);
print( foo ); // 3
foo = add("Oh", " baby");
print( foo ); // Oh baby
In the first case Java performed arithmetic addition on the integers 1 and 2.
(By the way, if we had passed in
numbers of other types BeanShell would have performed the appropriate numeric promotion
and returned the correct Java primitive type.)
In the second case BeanShell performed the usual string concatenation for String
types and returned a String object.
This example is a bit extreme,
as there are no other overloaded operators like string concatenation in Java.
But it serves to emphasize that BeanShell methods can work with loose types.
在第一个例子中Java执行整数1和2的算术加法。(通过这种方法,如果我们输入其它类型的
数字,Beanshell将会执行适当的程序并且返回正确的Java基础类型。)。在第二个例子中,
Beanshell执行Java中的字符串连接并且返回一个字符串对象。
这个例子有一些极端,就像在Java中没有连接字符串的操作符一样。
但是这个强调了Beanshell的方法可以使用宽松类型定义。
Methods with unspecified return types may return any type of object (as in the previous example).
Alternatively they may also simply issue a "return;"
without a value, in which case the effective type of the method is "void" (no type).
In either case, the return statement is optional.
If the method does not perform an explicit "return" statement
and the return type is not explicitly set to void,
the value of the last statement or expression in the method body becomes the return value
(and must adhere to any declared return typing).
没有定义返回类型的方法可以返回任何的对象类型(就像前面的例子中)。
也可以没有返回值而仅仅简单的返回“return”,就是返回类型为void的这种情况
在任何一个例子中,返回表达式是可选的。
如果函数没有定义一个显式的"return"表达式并且返回类型没有显式的定义为void,
函数中的最后一个定义或表达式变成返回值(并且附着在一些定义的返回类型上面)。
2.Method Modifiers and 'throws' Clauses 函数调用和"thros"语句
The standard Java modifiers may be applied to methods:
private / protected / public, synchronized, final, native, abstract, and static.
The synchronized modifier is the only modifier currently implemented.
The others are ignored.
The 'throws' clause of methods is checked for valid class type names,
but is not otherwise enforced.
Synchronized methods are synchronized on the object representing the method's common parent scope,
so they behave like Java methods contained in a class.
We will return to this topic after discussing scrīpted
标准的Java定义可以应用在函数上:
private / protected / public, synchronized, final, native, abstract, and static.
同步修改是目前已经实现的修改,其它的还没有。
带"throws"语句的方法会检查有效类名,但是不会被另外强迫。
同步方法是同步的,在本对象的父亲对象的作用域中,他们就像Java方法被包含在一个类中一样。
再讨论了脚本之后,我们将再回到这个观点。
3.scrīpted Methods 脚本函数
objects and "closures".
// foo() and bar() are synchronized as if they were in a common class
//如果是在一个普通的类中,foo()和bar()是同步的。
synchronized foo() { }
synchronized bar() { }
Scoping of Variables and Methods 变量和函数的作用域
As in Java, a method can refer to the values of variables and method names from the enclosing scope
(in Java the "enclosing scope" would be a class). For example:
就像在Java中一样,一个方法可以指向变量和函数的值(在Java中"封闭的作用域"可以是一个类)。示例:
a = 1;
anotherMethod() { ... }
foo() {
print( a );
a = a+1;
anotherMethod();
}
// invoke foo() 调用foo()
foo(); // prints 1
print( a ); // prints 2
Variables and methods are "inherited" from the parent scope in the usual way.
In the example above there are just two levels of scope:
the top or "global" scope and the scope of the method foo().
Later we'll talk about scrīpting objects in BeanShell
and see that there can be arbitrary levels of scoping involved.
But the rules will be the same.
变量和方法在通常的情况下是继承父类的作用域。
在上面的例子中,仅仅存在两种作用域:
全局作用域和方法foo()中的作用域。稍候我们谈谈在Beanshell中的脚本对象,
并且可以看到任意级别的作用域。但是这个规则是一样的。
As in Java, a typed variable is not visible outside the scope in which it is declared.
So declaring a variable with a type is a way to limit its scope or make a local variable.
In BeanShell using an untyped or "loosely" typed variable is also equivalent to declaring a local variable.
That is, if you use a variable that has not been defined elsewhere,
it defaults to the local scope:
就像在Java中一样,一个有类型的变量在作用域的外面是不可见的。
所以定义一个有类型的变量是限制它的作用域或者定义一个本地变量的方法。
在Beanshll中使用一个宽松类型的变量也等价于定义一个本地变量。
那也就是,如果你在使用一个在其它地方没有定义的变量,它默认是本地变量:
a = 1;
foo() {
a = a + 1; // a is defined in parent scope a有父亲作用域。
b = 3; // undefined, defaults local scope 没有定义,默认本地作用域
int c = 4; // declared local scope 定义本地变量
}
// invoke foo()
print( a ); // prints 2
print( b ); // ERROR! b undefined 错误!b没有定义
print( c ); // ERROR! c undefined 错误!c没有定义
In the above example the variable 'a' is declared in the global scope.
When its value is read and assigned inside of foo() the global value of 'a' will be affected.
The variable 'b' is a usage of an untyped variable.
Since 'b' has not been declared or assigned a value in any enclosing scope,
it becomes a local variable 'b' in the scope of foo.
The variable 'c' is explicitly declared (with a type) in the scope of foo() and is therefore,
of course, local to foo().
在上面的例子中,变量"a"是定义的全局变量。
在foo()中全局变量"a"的值将被改变。
变量"b"是一个没有定义类型的变量,"b"在一个封闭的作用域中没有定义类型,
它将变成foo中的一个本地变量。
在foo()中变量"c"被显式定义为一个有类型的变量,所以它是一个本地变量。
Later we'll see that BeanShell allows arbitrary nesting of methods.
If we were to declare another method
inside of foo() it could see all of these variables (a, b, and c)
as it is also in the scope of foo().
稍后我们将看到Beanshll允许任意的内置函数,
如果我们在foo()中定义另一个函数,它将可以调用所有的这些变量(a,b和c),
就像在foo()中一样。
4.Scoping of Loosely Typed Variables 宽松类型变量的作用域
As in Java, declaring a variable with a type will always make it local.
Even if the variable exists in the outer scope,
it will be hidden by the local variable declaration.
But what of loosely typed variables? As we've seen,
untyped variable usage looks just like an ordinary Java assignment.
What do we do if we want to make a local variable with the same name as a global one?
One answer would be to resort to declaring the variable with a type.
But if we wish to continue working with loosely typed variables in this case we have two options:
We can explicitly declare a loosely typed variable with the BeanShell 'var' type.
Or we can simply qualify our assignment with the 'this.' qualifier.
就像在Java中一样,通常定义一个有类型的本地变量。
如果这个变量存在外面的作用域中,它将被本地定义的变量隐藏。
但是什么是宽松类型变量呢?就像我们看到的一样,
宽松类型变量使用看起来像一个普通Java定义。
如果我们想让一个本地变量和一个全局变量有同样的名字,我们应该做什么?
答案之一就是你应该将这个变量定义为有类型的变量。
但是如果我们希望在这种情况下定义宽松类型变量,我们将有两种选择:
我们可以显式的定义一个Beanshll中的宽松类型"var".
或者我们可以简单的将变量定义在this对象中。
If you wish to, you can explicitly declare an untyped variable (making it local) using the special type 'var'. e.g.
如果你想的话,你可以使用"var"显式的定义一个没有类型的变量:
foo() {
var a = 1;
}
foo();
print( a ); // ERROR! a is undefined! 错误!a没有定义
'var' is a magic type in BeanShell that represents a loose (untyped) variable.
The default value of a variable declared with 'var' is null.
Alternately, you can use the scope modifier 'this' to explicitly qualify the variable assignment and make it
local.
'var'是一个神奇的类型,是Beanshll中用来定义宽松类型变量。
定义为'var'类型的变量的默认值为null.
你也可以使用'this'显式的定义一个变量并且使它本地化。
foo() {
this.a = 1;
}
foo();
print( a ); // ERROR! a is undefined! 错误!a没有定义
In this example we used the modifier 'this' to qualify an untyped variable's scope and make it local.
We will
explain 'this' and what it means in BeanShell scrīpted methods in the next section on scrīpted Objects.
在这个例子中,我们使用'this'显式的定义一个变量并且使它本地化。
在下面的章节中,我们将解释'this',在Beanshell脚本函数中它的含义是什么。
5.Scope Modifier: 'super' 作用域修改:'super'
Within a method, it is possible to explicitly qualify a variable or method reference with the identifier 'super' in
order to refer to a variable or method defined in an enclosing scope
(the scope in which the method is defined
or "higher"). e.g.
在一个函数中,为了调用一个封闭的作用域(一个更高的作用域)中的变量或者函数,
可以显式的定义一个变量或者函数指向'super'
int a = 42;
foo() {
int a = 97;
print( a );
print( super.a );
}
foo(); // prints 97, 42
Scoping of Loosely Typed Variables 宽松类型变量的作用域
As in Java, the 'super' modifiers tells the scoping to begin its search for the variable or method in the parent
scope.
In the case above, the variable 'a' by default refers to the variable in the local scope.
By qualifying 'a'
with 'super' we can refer to the variable 'a' in the global scope (the "topmost" scope).
So, we've seen that 'super' can be used to refer to the method's parent context.
We'll see in the next section
how 'this' and 'super' are used in scrīpting Objects in BeanShell.
就像在Java中一样,'super'的作用就是在更高的作用域搜索变量或者函数。
在上面的例子中,变量'a'指向一个本地是一个本地变量(代码print(a)).
通过'super'来定义变量'a',我们可以指向全局的变量'a'(最高的作用域).
于是,我们可以看到,'super'可以用作指向函数的外部环境。
在下面的章节中,我们也可以看到在Beanshll的脚本对象中如何应用'this'和'super'.
6.scrīpted Objects 脚本对象
Many people who use BeanShell use it to write scrīpts that work with existing Java classes and APIs,
or perform other kinds of dynamic activities for their own applications at run.
time without the aid of a compiler.
Often this means writing relatively unstructured code .
for example, a sequence of method invocations or loops,
all contained in a single scrīpt file or eval() statement.
In the previous section we saw that BeanShell is also capable of scrīpting methods,
just like Java.
Creating methods and new BeanShell commands (which are
just methods in their own files)
is the natural progression of organizing your scrīpts into re.usable and
maintainable components.
许多人使用Beanshell来写和Java类和API一起工作的脚本。
或者在他们自己的在运行的应用程序中执行其它种类的动态活动,并且不需要编译。
这个意思常常是说写非结构的代码。
例如,一序列方法调用或循环,所有包含在单脚本文件中或者执行表达式。
在上面的章节中,我们看到也可以使用脚本函数,就像Java中一样。
创建函数和新Beanshell命令(正好函数在他们自己的文件中)
是很自然的进展,并且可以组织您的脚本到re.usable和变成可维护的组件。
Beyond methods and structured programming lie, of course,
objects and the full breadth of object oriented programming.
In Java objects are the products of classes.
While BeanShell is compatible with standard Java syntax for statements,
expressions, and methods, you can't yet scrīpt new Java classes within BeanShell.
Instead, BeanShell allows you to scrīpt objects as "method closures",
similar to the way it is done in Perl 5.x,
Javascrīpt, and other object.capable scrīpting languages.
This style of scrīpting objects (which we'll describe
momentarily) is simple and flows very naturally from the style of scrīpting methods.
The syntax, as you'll see,
is a straightforward extension of the standard Java concept of referring to an object with a 'this' reference.
超越了方法和结构化编程,对象可以引导编程,在Java中对象是类型的实例。
Beanshell和标准的Java语法是一致的,在语句申明,表达式和方法方面,
但是你不可以在Beanshll中定义新的类型。可以这样来替代,
Beanshell允许你类似于"method closures"一样的定义对象,
和Perl5.x,Javascrīpt等支持对象的脚本语言中的实现相似。
这种样式的脚本对象很简单并且用对象来表示脚本函数很自然。
你看到的这种语法,是一种通过this引用指向一个对象的标准Java概念的扩展。
Note: 注意:
In standard Java, a method inside of an object (an instance method) may refer to the enclosing
object using the special variable 'this'. For example:
在标准的Java中,一个对象中的方法通过使用变量'this'可以指向装入的对象。例如:
// MyClass.java
MyClass {
Object getObject() {
return this; // return a reference to our object 返回一个引用
}
}
In the example above, the getObject() method of MyClass returns a reference to its own object
instance (an instance of the MyClass object) using 'this'.
在上面的例子中,MyClass中的getObject()方法使用'this'返回一个引用给它自己的对象实例。 


相关阅读:

TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

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

RSS订阅

Open Toolbar