【发布时间】:2013-03-15 17:10:15
【问题描述】:
以下 Groovy/Gremlin sn-ps 有什么区别? (都保存为 *.groovy 文件并使用./gremlin.sh -e [filename].groovy 运行)
class user
{
String username
static void main(String[] args)
{
user mtm = new user()
mtm.username = "MuffinTheMan"
println mtm.username
}
}
和
class User
{
String username
static void main(String[] args)
{
User mtm = new User()
mtm.username = "MuffinTheMan"
println mtm.username
}
}
第一个给出了 3 个类似于这个的编译错误:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 7: Apparent variable 'mtm' was found in a static scope but doesn't
refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from
a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'mtm' but left out brackets in a place not allowed
by the grammar.
@ line 7, column 14.
user mtm = new user()
第二个编译并运行良好并输出:
MuffinTheMan
【问题讨论】: