
enum ProductType { CASH_BONUS { voID doSomething() {} },CUSTOMIZABLE { voID doSomething() {} } abstract voID doSomething()} 但是当我尝试在Groovy控制台中运行它时,我得到错误:
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: -1Can’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
我似乎记得读过Groovy(尚未)支持枚举常量的重写方法,这是正确的,如果是这样,是否有一种优雅的方式来模拟这种行为?
更新
这是一个围绕Groovy 1.8.0修复的错误
解决方法 这是一个错误: http://jira.codehaus.org/browse/GROOVY-4641你可以使抽象方法不抽象.抛出异常以确保始终覆盖它,如:
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(); 总结 以上是内存溢出为你收集整理的groovy – 覆盖抽象枚举方法全部内容,希望文章能够帮你解决groovy – 覆盖抽象枚举方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)