开发者

override abstract enum method

The following is valid Java code:

enum ProductType {
  CASH_BONUS { 

     void doSomething() {}    
  },

  CUSTOMIZABLE { 
     void doSomething() {}    
  }

  abstract void doSomething()
}

But when I try to run this in the Groovy console, I get the errors:

Can't have an abstract method in a non-abstract class. The class 'ProductType' must be declared abstract or the method 'void doSomething()' must be implemented. at line: -1, column: -1

Can't have an abstract method in a non-abstract class. The class 'ProductType' must be declared abstract or the method 'void doSomething()' must not be abstract. at line: 11, column: 3

I seem to recall reading that Groovy does not (yet) support overriding methods for enum cons开发者_运维知识库tants, is this correct, and if so, is there an elegant way to emulate this behavior?

Update

This was a bug that was fixed some time around Groovy 1.8.0


it's a bug: http://jira.codehaus.org/browse/GROOVY-4641

you can make the abstract method not abstract. throw an exception to make sure you always override it like:

enum ProductType {
    CASH_BONUS(1) {
        void doSomething() {
        }
    },
    CUSTOMIZABLE(2) {
        void doSomething() {
        }
    };
    ProductType(int n) {
        this.n=n;
    }
    final int n;
    void doSomething() {
        throw new UnsupportedOperationException()
    }
}

ProductType.CASH_BONUS.doSomething();
ProductType.CUSTOMIZABLE.doSomething();


Update the Groovy Compile from 1.8 to 2.0 in eclipse worked for me

(Eclipse 3.7)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜