Beanshell翻译7

 1.The 'this' reference 'this'引用
As in most languages,
an executing method in BeanShell has its own "local" scope that holds argument
(parameter) variables and locally declared variables.
For example, in the following code segment any
variables that we might use within the foo() method will normally only be visible within the scope of foo()
and for the lifetime of one particular foo() method invocation:
就像大多数的语言一样,在Beanshell中一个生效的方法有它自己的本地作用域,
这个作用域包括参数变量和本地定义变量。
例如,在下面的代码片段中,在foo()方法中用到的一些变量将仅仅在foo()方法内可见。
// Define the foo() method: 定义foo()方法
foo() {
int bar = 42;
print( bar );
}
// Invoke the foo() method: 调用foo()方法
foo(); // prints 42
print( bar ); // Error, bar is undefined here 错误,在这里bar没有定义
In the above, the bar variable is local to foo() and therefore not available outside of the method invocation .
it is thrown away when the method exits, just like a standard Java local variable.
在上面,bar变量是foo()中的本地变量,所以在方法外面不可用。
当方法退出的时候,它会被抛弃,就像标准语法中的本地变量一样。
Now comes the twist .
In BeanShell you have the option to "hang on" to the scope of a method invocation
after exiting the method by referring to the special 'this' reference.
As in Java, 'this' refers to the current object context.
The only difference is that in this case the context is associated with the method
and not a class instance.
By saving the 'this' reference after the method returns,
you can continue to refer to variables defined within the method,
using the standard Java "." notation:
现在来看看这个用法。在Beanshll中,在推出函数之后,通过'this'引用
,你可以选择挂在函数的作用域中。就像在Java中一样,'this'指向当前环境对象。
唯一的区别是,在这个例子中,环境是和这个方法联系起来了,而不是一个类实例。
在函数返回之后,通过保存'this'引用,使用标准的Java中的'.'符号,
你可以继续方法定义在这个函数中的对象。
foo() {
int bar = 42;
return this;
}
fooObject = foo();
print( fooObject.bar ); // prints 42!
In the above, the value returned by the foo() method (the 'this' reference)
can be thought of as an instance of a "foo" object.
Each foo() method invocation effectively creates a new object;
foo() is now not just a method, but a kind of object constructor.
In the above case our foo object is not so much an object,
but really more of a structure.
It contains variables (bar) but no "behavīor".
The next twist that we'll introduce is that BeanShell methods are also allowed to
contain other methods:
在上面的例子中,通过foo()函数返回的值('this'引用)可以理解为一个'foo'对象的实例。
每一个foo()函数马上创建为一个新的对象;foo()现在不仅是一个函数,而是一个对象的构造函数。
在上面的例子中,我们的foo对象不是一个对象,实际上是一个结构。
它包含了变量bar但是没有行为。
下面的用法中,我们将介绍,Beanshll函数也允许包含其它的函数:
foo() {
bar() {
...
}
}
scrīpted methods may define any number of nested methods in this way,
to an arbitrary depth. The methods are "local" to the method invocation.
用这种方法,脚本函数可以定义任意深度的内置函数,这些内置函数都是本地的。
Statements and expressions within the enclosing BeanShell method can call their "local" methods just like any
other method.
(Locally declared methods override outer.more methods like local variables hide instance
variables in Java.)
The enclosed methods are not directly visible outside of their enclosing method.
However, as you might expect, we can invoke them as we would on a Java object,
through an appropriate object reference:
在函数中的申明和表达式可以访问他们的本地函数。
(本地函数覆盖外部函数就像本地变量覆盖Java中的实例变量一样)。
在函数的外面,内部函数是不可见的。
但是,就像你期望的一样,我们可以通过适当的对象引用调用它们,就像在Java中一样。
foo() {
int a = 42;
bar() {
print("The bar is open!");
}
bar();
return this;
}
// Construct the foo object 构造foo对象
fooObject = foo(); // prints "the bar is open!" 打印"the bar is open!"
// Print a variable of the foo object 打印foo对象中的一个变量
print ( fooObject.a ) // 42
// Invoke a method on the foo object 调用foo对象的函数
fooObject.bar(); // prints "the bar is open!" 打印"the bar is open!"
scrīpted Objects 脚本对象
method. i.e. there are no block.local methods. For example:
在一个程序块中没有本地函数。例如:
foo() {
bar() { }
if ( true ) {
bar2() { }
}
return this;
}
2.Scope Modifiers 作用域修改
Now that we've seen how methods can be nested and treated as objects,
we can revisit the topic of variable scope and scope modifiers.
现在我们可以看到,函数如何变成内置的和怎样理解成一个对象,
我们可以用新的方法来访问变量。
3.'this', 'super', and 'global' 'this','super',和'global'
In the "scrīpted Methods" section we described the use of 'super' to refer to a method's parent scope
(the scope in which the method is defined).
And in the previous section we talked about super's brother 'this',
which refers to the current method's scope,
allowing us to think of a method scope as an object.
Now we can see how these concepts are related.
Any method scope, and indeed the 'global' scope, can be thought as an object context.
A scrīpted object can be thought of as encapsulated in a parent scope that determines its
"environment" . its inherited variables and methods.
在脚本函数这个章节中,我们使用'super'指向函数的父作用域.
并且在前一个章节中,我们谈到了'super'的兄弟'this',
'this'指向当前的方法作用域,允许我们将一个函数理解为一个对象。
现在我们可以看到这种观点怎样联系起来的.
一些方法的作用域,当然是'global'的作用域,可以理解为一个对象环境。
一个脚本对象可以理解为它的父作用域中的一个独立个体,在父作用域中包括变量和函数。
The references 'this', 'super', and 'global' are really the same kind of reference .
references to BeanShell method contexts, which can be used as scrīpted objects.
From here on We'll refer to 'this', 'super', 'global',
and any other reference to a scrīpted object context in general as a 'this' type reference.
'this','super'和'global'是同样类型的引用。指向Beanshell中函数的引用可以用在脚本对象中。
从这里,我们可以指向'this','super','global',
并且其它的常用的指向脚本对象的引用可以当成'this'类型的引用。
Note: 注意:
If you print a 'this' type reference you'll see what it refers to:
如果你打印一个'this'类型引用,你将可以看到它指向的是什么:
BeanShell 1.3 . by Pat Niemeyer ( pat@pat.net)
bsh % print( this );
'this' reference (XThis) to Bsh object: global 'this'引用指向Bsh对象:global
bsh % foo() { print(this); print(super); }
bsh % foo();
'this' reference (XThis) to Bsh object: foo 'this'引用指向Bsh对象:foo
'this' reference (XThis) to Bsh object: global 'this'引用指向Bsh对象:global
The above note shows that the foo() method's 'this' reference is local (named 'foo')
and that it's parent is the global scope;
the same scope in which foo is defined.
上面的代码段表明了,foo()函数的'this'引用是本地的(名字为'foo'),并且它的父亲是global作用域;
global是foo被定义的作用域。
4.'global'
The scope modifier 'global' allows you to always refer to the top.most scope.
In the previous note you can see that the top level scrīpt context is called "global"
and that it appears again as the 'super' of our foo() method.
The global context is always the top scope of the scrīpt.
It is the global namespace of the current interpreter.
Referring to 'super' from the top scope simply returns the same 'global' again.
'global'允许你指向最顶层的作用域。
在上面的例子中,你可以看到,最顶层的脚本环境叫'global',并且以'super'出现在foo()函数中.
global环境常常是脚本的顶层作用域,它是当前解释器的global名字空间。
从上层作用域指向'super'也是很简单的返回'global'.
global.foo = 42;
Global variables are not special in any way.
Their visibility derives simply from the fact that they are in the topmost scope.
However, for those who do not like the idea of qualifying anything with "global".
You can always use a more object oriented approach like the following.
Global变量一点也不特殊。它的可见性是简单的来自这个事实,它们是在最顶层的作用域中。
但是,对于那些不喜欢使用'global'的人来说,你也可以像下面一样使用一些对象来表示。
// Create a top level object to hold some state 创建一个最顶层的对象来保存状态
dataholder = object();
foo() {
...
bar() {
dataholder.value = 42;
}
bar();
print( dataholder.value );
}
}
In the above example we used a global object to hold some state,
rather than putting the 'value' variable directly in the global scope.
在上面的例子中,我们使用一个global对象去保存状态,比将变量的值直接放在global作用域中更恰当。
Tip: 提示
In the above example we used the BeanShell object() command
to create an "empty" BeanShell scrīpted object context in which to hold some data.
The object() command is just a standard empty method named object() that returns 'this'.
The variable 'dataholder' above is a 'this' type reference and has all of the
properties of any other BeanShell object scope.
在上面的例子中,我们使用Beanshll中的object()命令,创建一个可以保存数据的空的Beanshell脚本对象。
object()命令仅仅是一个标准的返回'this'的空方法。
上面的变量'dataholder'是一个'this'类型的引用并且有Beanshll对象作用域的所有属性。
5.Synchronized Methods Revisited 同步的方法更新
Now that we have covered the meaning of 'this' and 'super' with respect to BeanShell methods,
we can define the meaning of the 'synchronized' modifier for BeanShell methods.
Synchronized BeanShell methods behave as if
they were in a common class by synchronizing on their common 'super' reference object.
For example, in the four cases in the following example,
synchronization occurs on the same Java object.
That object is the 'this' type reference of the global scope
(a Beanshell object of type bsh.This):
现在我们可以通过'this'和'super'来表示Beanshell函数,我们在Beanshell函数上面加上同步修改。
同步Beanshell函数行为就像在它们的'super'引用上加同步一样。
例如,在下面的四个例子中,同步出现在同一个Java对象上,
这个对象就是global作用域上面的'this'类型引用(一个类型为bsh.This的Beanshell对象)
print( this );
// 'this' reference (XThis) to Bsh object: global 表示Bsh对象(global)的'this'引用
// The following cases all synchronize on the same lock 下面的例子都是相同级别的同步
synchronized ( this ) { } // synchronized block 同步程序块
synchronized int foo () { } // synchronized method foo() 同步函数foo()
synchronized int bar () { } // synchronized method bar() 同步函数bar()
int gee() {
synchronized( super ) { } // synchronized blockinside gee() 同步gee()中的程序块

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值