Groovy枚举:switch语句在与静态相关的运行之前引发错误

时间:2019-05-29 14:33:53

标签: groovy enums

以下代码可以在Intellij中很好地编译,但是当尝试将Groovy用作脚本时,甚至在运行之前它都会引发错误。 我找不到裂缝,因为实际上一切都是静态的?

public enum OutputType {
        ABC,
        DEF,
        GHI
    }
    //Just initializing here
        public static OutputType output=OutputType.ABC;



    public static void run() {

        switch (output){
            case ABC:
                runABC();
                break;

            case DEF:
                runDEF();
                break;

            case GHI:
                runGHI();
                break;

            default:
                break;
        }
    }

错误:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 50: Apparent variable 'ABC' 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 'ABC' but left out brackets in a place not allowed by the grammar.
 @ line 50, column 18.
               case ABC:
                    ^

Script1.groovy: 54: Apparent variable 'DEF' 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 'DEF' but left out brackets in a place not allowed by the grammar.
 @ line 54, column 18.
               case DEF:
                    ^

Script1.groovy: 58: Apparent variable 'GHI' 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 'GHI' but left out brackets in a place not allowed by the grammar.
 @ line 58, column 18.
               case GHI:

2 个答案:

答案 0 :(得分:1)

您是否尝试过从脚本中删除静态内容并在情况下添加枚举类名称?

    switch (output){
        case OutputType.ABC:
            runABC();
            break;

        case OutputType.DEF:
            runDEF();
            break;

        case OutputType.GHI:
            runGHI();
            break;

        default:
            break;
    }

答案 1 :(得分:1)

因此,我尝试删除之前的所有静态语句。 现在有一个新错误。不幸的是我不得不擦掉类名:

groovy.lang.MissingPropertyException: No such property: OutputType for class: XXXXXXXXXXX.tempscripts.Script1

由于这是另一个问题,我在上面将答案标记为正确。 谢谢!