Beanshell翻译5

 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包中的一部分)是后导入的,所以它有更高的优先级。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Beanshell脚本是一种基于Java语言的脚本语言,它可以在Java虚拟机中运行。Beanshell脚本具有Java语言的所有特性,包括面向对象、异常处理、反射等。与Java相比,Beanshell脚本更加灵活和简洁,可以快速编写和调试脚本。Beanshell脚本可以用于编写自动化测试脚本、批处理脚本、系统管理脚本等。 ### 回答2: BeanShell是一种脚本语言,旨在提供一种简单的方式来编写和执行Java代码。它可以在Java虚拟机中运行,并且与Java代码可以无缝集成。 Beanshell脚本具有以下特点: 1、易于学习和使用:Beanshell基于Java语法,因此对于熟悉Java的开发人员来说,学习和使用Beanshell非常简单。 2、动态类型:Beanshell支持动态类型,这意味着在脚本中可以直接使用变量而不需要事先声明其类型。 3、直接访问Java类库:Beanshell可以直接访问Java类库中的类和方法,在脚本中可以方便地使用Java类和对象。 4、交互式解释器:Beanshell还提供了一个交互式解释器,可以在其中逐行执行脚本代码,并查看结果。 5、与Java的无缝集成:Beanshell脚本可以与Java代码无缝集成,可以将Beanshell脚本嵌入到Java应用程序中,并在运行时动态执行。 6、易于扩展:Beanshell脚本可以通过自定义Java类和方法进行扩展,以满足特定应用的需求。 通过Beanshell脚本,开发人员可以更加灵活地编写和执行Java代码,因为它提供了一种简单且易于使用的脚本语言,同时还能直接访问Java类库和与Java代码无缝集成。这使得Beanshell成为在Java应用程序中进行快速原型开发、测试和调试的强大工具。无论是构建大型企业应用程序还是编写简单的脚本,Beanshell都是一个非常有用的脚本语言。 ### 回答3: BeanShell是一种用于Java虚拟机的小型编程脚本语言。它具有简洁、灵活的语法结构,并且可以直接在Java环境中运行。Beanshell脚本通常以.bsh为后缀名。 Beanshell脚本可以被用于多种不同的应用场景。首先,它可以作为脚本语言嵌入到Java应用程序中使用。开发人员可以通过编写Beanshell脚本来动态执行一系列Java语句,从而实现应用程序的自定义逻辑。这对于需要灵活性和自定义性的应用程序非常有价值。 其次,Beanshell脚本也可以用于Java环境的交互式编程。开发人员可以使用Beanshell脚本解释器来实时执行和测试Java代码片段,并查看实际的执行结果。这对于调试和学习Java代码非常有帮助。 另外,Beanshell还支持面向对象编程和Java的标准库。它可以轻松地调用Java类和方法,并使用Java的内建函数。这使得Beanshell脚本能够处理复杂的逻辑和数据操作。 最后,在某些情况下,Beanshell脚本可以作为一种脚本语言来代替Java编写一些简单的任务。Beanshell脚本在语法和函数方面比Java更加简洁,使得一些简单的任务可以更快速地完成。 总之,Beanshell脚本是一种灵活、易用的脚本编程语言,它可以嵌入到Java应用程序中,用于自定义逻辑、交互式编程以及简化一些简单的任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值