Groovy的语言特点
2010-09-24 星期六 晴朗
与目前主流的脚本语言一样,all values are objects。
与Ruby和Perl的区别:不是使用赋值代替类型声明和推断。而是使用显示的def关键词定义变量,这样可以避免作用域的问题。
"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use "def" in replacement. This is needed to the make variable definitions detectable for the Groovy parser.
def dynamic = 1
dynamic = "I am a String stored in a variable of dynamic type"
int typed = 2
typed = "I am a String stored in a variable of type int??" // throws ClassCastException