【问题标题】:Groovy/Gremlin class naming scheme (capital letter required)Groovy/Gremlin 类命名方案(需要大写字母)
【发布时间】: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

【问题讨论】:

    标签: groovy gremlin


    【解决方案1】:

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

    如果其他人有更好的链接指向关于该问题的更全面/清晰的讨论,请发布!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2011-07-12
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多