2013-03-15 15 views
0

下面的Groovy/Gremlin片段有什么区别? (均保存为* .groovy文件,并与./gremlin.sh -e [filename].groovy运行)Groovy/Gremlin类的命名方案(需要大写字母)

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 

回答

0

原来唯一的区别是第一个类名以小写字母开头,第二个类名以大写字母开头。有一些讨论here,但在这个问题上很难找到很多有用的信息。我决定发布这篇文章是因为我正在打我的头靠在墙上(而不是字面意思)试图弄清楚为什么我的代码(类名小写的第一个字母)不能编译,也许其他人可能会遇到这个问题。

如果其他人有更好的链接,就此问题进行更全面/清晰的讨论,请发帖!