Groovy快速入门
1. 脚本与类中变量引用的区别
在编写Groovy类和脚本时,有一个重要区别需要记住:Groovy脚本对变量引用有特殊的绑定。在脚本中,我们可以在初始化变量后立即使用它,而无需事先声明。脚本变量存储在 Binding 作用域中。例如:
savings = new Account(id:2, balance:0.00, owner:customer)
这里, savings 变量会自动添加到 Binding 作用域。当使用 savings 时:
println savings
此时, savings 必须在 Binding 作用域中,否则会出错。
如果将上述示例的脚本部分重写为包含类定义和 main 方法,那么 savings 和 customer 必须使用 def 关键字显式定义:
class AccountSample {
public static void main (args) {
def customer = new Custome
超级会员免费看
订阅专栏 解锁全文
27

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



