Beanshell翻译5

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

Beanshell翻译5

http://user.qzone.qq.com/281696143/blog/1216972587  Ronger
1.In BeanShell you may access JavaBean properties as if they were fields:
在Beanshell中你可以访问JavaBean中的属性:
button = new java.awt.Button();
button.label = "my button"; // Equivalent to等价于: b.setLabel("my button");
print( button.label ); // Equivalent to 等价于 print( b.getLabel() ); 等价于
JavaBean properties are simply pairs of "setter" and "getter" methods
that adhere to a naming convention.
In the above example BeanShell located a "setter" method with the name "setLabel()"
and used it to assign the string value.
It then found the method named getLabel() to retrieve the value.
JavaBean的属性是一类简单的"setter"和"getter"方法。在上面的例子中,Beanshell通过名字
"setLabel()"来定为一个"setter"方法,并且通过它来赋值,通过getLable()方法取值。
Boolean properties may optionally use the syntax "is" for their "getter". e.g.
Boolean属性可以使用"is"来代替"getter"
Float f = new Float(42f);
print( f.infinite ); // Equivalent to 等价于 print( f.isInfinite() ); // false
If there is any ambiguity with an actual Java field name of the object
(e.g. label in the above example) then the
actual field name takes precedence.
If you wish to avoid any ambiguity BeanShell provides an additional,
uniform syntax for accessing both Java Bean properties and Hashtable or Map entries.
You may use the "{}" curly brace construct
with a String identifier as a qualifier on any variable of the appropriate type:
如果和实际的对象的属性名字有不明确的地方(比如上面的例子中的label),
将优先考虑实际的属性名称。如果你希望避免Beanshell中不明确的地方。
Hashtable,Map和JavaBean属性都可以用使用。
你可以使用"{}"构造一个字符串来限定适当类型的变量。
b = new java.awt.Button();
b{"label"} = "my button"; // Equivalent to 等价于: b.setLabel("my button");
h = new Hashtable();
h{"foo"} = "bar"; // Equivalent to等价于: h.put("foo", "bar");
Where the java.util.Collections API is available, Maps are also supported.
java.util.Collection已经导入,Maps也是可以支持的。
2.Enhanced 'for' Loop 支持循环
BeanShell supports the Java 1.5 style enhanced for.loop for iterating over collections and array types.
(Note that you do not have to be running Java 1.5 to use this feature).
Beanshell支持Java1.5以上的for.loop功能,用来遍历集合和数组。
(注意你不能通过运行Java1.5来使用这个特性)
List foo = getSomeList();
for ( untypedElement : foo )
print( untypedElement );
for ( Object typedElement: foo )
print( typedElement );
int [] array = new int [] { 1, 2, 3 };
for( i : array )
print(i);
for( char c : "a string" )
print( c );
Supported iterable types include all the obvious things. 支持包含下面这些集合类型
· JDK 1.1+ . (no collections): Enumeration, arrays, Vector, String, StringBuffer
Convenience Syntax 便利的语法
· JDK 1.2+ . (w/collections): Collections, Iterator
See also the BshIterator API which supports the ehanced for.loop and allows iteration over these types using
the dynamically loaded BeanShell Collection manager.
也可以看到BshIterator API,支持for.loop并且通过使用动态加载Beanshell集合管理器来允许遍历更多的类型。
3.Switch Statements 选择语句
In BeanShell, the switch statement may be used not only with numeric types but with objects.
For example,
you may switch on Dates and Strings which are compared for equality with their equals() methods:
在Beanshell中,选择语句不仅可以用在数字类型中,也可以用于对象。
比如,你可以通过equals()方法来比较Dates和Strings类型。
dateobj = new Date();
switch( dateobj )
{
case newYears:
break;
case christmas:
break;
default:
}
4.Auto Boxing and Unboxing 自动包装和拆包
"Boxing" and "Unboxing" are the terms used to describe automatically wrapping a primitive type in a wrapper
class and unwrapping it as necessary.
Boxing is a feature of Java (SDK1.5) and has been supported in
BeanShell for many years.
BeanShell supports boxing and unboxing of primitive types. For example:
"包装"和"拆包"是一种技术,当需要的时候,这种技术是用来自动包装基础类型和拆包的。
包装是一个Java1.5的特性并且Beanshell很多年前就可以支持这些。Beanshell支持
基础类型的包装和拆包。例如:
int i=5;
Integer iw = new Integer(5);
print( i * iw ); // 25
Vector v = new Vector();
v.put(1);
int x = v.getFirstElement();
Importing Classes and Packages 导入类和包
In BeanShell as in Java, you can either refer to classes by their fully qualified names,
or you can import one or more classes from a Java package.
在Beanshll中就像和在Java中一样,你可以通过类的全名来导入一个类,
或者你可以从一个包中导入一个或多个类。
// Standard Java 标准Java
import javax.xml.parsers.*;
import mypackage.MyClass;
In BeanShell import statements may appear anywhere, even inside a method,
not just at the top of a file.
In the event of a conflict, later imports take precedence over earlier ones.
A somewhat experimental feature is the "super import".
With it you may automatically import the entire classpath, like so:
在Beanshll中导入一个申明可以用在任何地方,甚至在一个方法里面,
而不仅仅是在一个文件的顶部。在冲突的时候,后导入的优先于前面导入的。
一些新的特性是"super import"。通过它你可以自动的导入完整的类路径,像下面一样:
5.Switch Statements 选择申明
import *; import *;
The first time you do this BeanShell will map out your entire classpath;
so this is primarily intended for interactive use.
Note that importing every class in your classpath can be time consuming.
It can also result in a lot of ambiguities.
Currently BeanShell will report an error when resolving an ambiguous import from
mapping the entire classpath.
You may disambiguate it by importing the class you intend.
第一次你做这些,Beanshell将覆盖完整的类路径,所以这是主要的互动使用。
注意在通过你的类路径来导入会很消耗时间。它也会导入不明确的类。
当通过导入类路径而产生了一个不明确的导入时,Beanshell将产生一个错误。
你可以通过导入你想要的类来明确它。
Tip: 提示:
The BeanShell which() command will use the classpath mapping capability to tell you
where exactly in your classpath a specified class is located:
Beanshell的which()命令将在类路径中寻找一个确切的类型的完整路径:
bsh % which( java.lang.String );
Jar: file:/usr/java/j2sdk1.4.0/jre/lib/rt.jar
See "Class Path Management" for information about modifying the BeanShell classpath at run.
time with the addClassPath() or setClassPath() commands.
Also see "BeanShell Commands" for information about importing new BeanShell commands from the
classpath.
通过"Class Path Management"获得更多信息,在运行时修改Beanshell类路径,
通过addClassPath()或者setClassPath()命令。
也可以通过"Beanshell Commands"获得更多信息,从类路径中导入新的Beanshell命令。
6.Default Imports 默认导入
By default, common Java core and extension packages are imported for you.
They are, in the order in which they are imported:
通过默认导入,通用的Java核心和扩展包被导入进来。他们是以下的这些:
· javax.swing.event
· javax.swing
· java.awt.event
· java.awt
· java.net
· java.util
· java.io
· java.lang
Two BeanShell package classes are also imported by default:
两个Beanshll包中的类也被默认导入:
· bsh.EvalError
· bsh.Interpreter
Finally, we should mention that BeanShell commands may be imported from the classpath.
The default commands are imported in the following way:
最后,我们会说起,Beanshell的命令可以从类路径中导入。默认的命令是从下面的路径导入的:
importCommands("/bsh/commands");
We will discuss how to import your own commands in a later section.
我们将在后面的章节中讨论怎样导入你自己的命令。
Tip: 提示:
The classes java.awt.List and java.util.List are both imported by default.
Because java.util.List is imported later, as part of the java.util package,
it takes precedence. To access java.awt.List simply import it in,
or the java.awt package again your scrīpt. Later imports take precedence.
类型java.awt.List和java.util.List都是通过默认导入的。因为java.util.List
(作为java.util包中的一部分)是后导入的,所以它有更高的优先级。
 


相关阅读:

TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

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

RSS订阅

Open Toolbar