beanshall支持动态类型,方法可以有动态的参数和返回值,在调用的时候决定这些类型,并试图做正确的处理。这和javascript一样。
e.g.
[quote]
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).
[/quote]
这句话的意思是:如果方法体的最后一句不是return子句并且返回类型不是void,最后一句表达式的值会成为返回值。
e.g.
e.g.
add(a,b){
return a+b;
}
foo=add(1,2);//foo=3
foo=add("h","i");//foo="hi"
[quote]
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).
[/quote]
这句话的意思是:如果方法体的最后一句不是return子句并且返回类型不是void,最后一句表达式的值会成为返回值。
e.g.
add(a,b){
k=a+b;
}
foo=add(1,2);//foo=3
err(){
print(99);
}
foo=err();// Error: EvalError: Void assignment. : at Line: 1 : in file: <unknown file> : foo = err ( )
本文介绍了beanshell中动态类型的特性及应用,展示了如何利用动态类型进行灵活的编程,包括动态参数和返回值的使用,以及当方法未显式设置返回值时的行为。
1296

被折叠的 条评论
为什么被折叠?



