在Groovy脚本中导入内部枚举 [英] Import inner enum in Groovy script

查看:297
本文介绍了在Groovy脚本中导入内部枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Vehicles.groovy 中定义了一个Groovy类,其中包含一些内部枚举:

I have a Groovy class defined in Vehicles.groovy that contains some inner enums:

public class Vehicles {
  public enum Land {
    BICYCLE,
    CAR,
    TRAIN
  }

  public enum Water {
    SAILBOAT,
    MOTORBOAT
  }

  public enum Air {
    JET,
    HELICOPTER
  }
}

我想在脚本<$中引用这些枚举c $ c> run.groovy 与 Vehicles.groovy 在同一目录中。

I'd like to reference these enums in a script run.groovy in the same directory as Vehicles.groovy.

完全限定枚举实例有效。

import Vehicles
println Vehicles.Land.BICYCLE

import static Vehicles.Land
println Vehicles.Land.BICYCLE

import Vehicles.Land.*
println Vehicles.Land.BICYCLE

正确打印 BICYCLE

但是,我想引用 Land 枚举而不完全限定它。

However, I'd like to reference the Land enum without fully qualifying it.

我基本上尝试了静态/非静态,别名/非锯齿以及星形/非星形导入的每种组合。

I basically tried every combination of static/non-static, aliased/non-aliased, and star/non-star imports.

导入Vehicles.Land 导入静态Vehicles.Land。* (或导入Vehicles.Land作为土地)给出无法解决类错误。这似乎很奇怪,因为它们就是用Java所做的事情(如果我错了,请纠正我。)

import Vehicles.Land or import static Vehicles.Land.* (or import Vehicles.Land as Land) give unable to resolve class errors. This seems weird because they're what one would do in Java (correct me if I'm wrong.)

如果我尝试

import static Vehicles.Land
println Land.BICYCLE

import static Vehicles.Land as Land
println Land.BICYCLE

import Vehicles.Land.*
println Land.BICYCLE

,我得到了错误

Caught: groovy.lang.MissingPropertyException: No such property: Land for class: run
groovy.lang.MissingPropertyException: No such property: Land for class: run
        at run.run(run.groovy:2)

类似地,

import Vehicles.Land.*
println BICYCLE

给予

Caught: groovy.lang.MissingPropertyException: No such property: BICYCLE for class: run
groovy.lang.MissingPropertyException: No such property: BICYCLE for class: run
    at run.run(run.groovy:2)

Vehicles.groovy run.groovy 都添加包声明

所以...


  • Groovy对导入内部类有什么支持?为什么它与Java不同?

  • 如何让Groovy允许我引用非完全限定的内部枚举?

注意:我正在使用Groovy 1.8.6和Oracle JDK 1.8.0_45。

Note: I'm using Groovy 1.8.6 and Oracle JDK 1.8.0_45.

推荐答案

Groovy确实支持导入嵌套类,包括枚举。但是,如果要访问它们而没有完全资格,则需要以非静态方式(与Java不同)导入它们,或者明确地声明它们为静态:

Groovy does support import nested classes, including enums. However to access them without full qualification, you'll need to import them in a non-static manner (unlike Java), or explicitly declare them static:

// Explicitly declare Water and Air as static to demonstrate
public class Vehicles {
  public enum Land { BICYCLE, CAR, TRAIN }
  public static enum Water { SAILBOAT, MOTORBOAT }
  public static enum Air { JET, HELICOPTER }
}

// Non-static nested enum needs non-static import (unlike Java)
import Vehicles.Land
println Land.BICYCLE

// Explicitly static nested enum can be static imported
import static Vehicles.Water
println Water.SAILBOAT

// Explicitly static nested enum can also be non-static imported as well!
import Vehicles.Air
println Air.JET

工作示例: https://groovyconsole.appspot.com/script/5089946750681088

不同于Java,其中枚举是隐式静态的,似乎Groovy中的枚举不是不是隐式静态的,因此为什么静态导入无法工作。这是因为Groovy中的枚举实际上与Java中的枚举不同,因此它们将增强功能。不幸的是,它们似乎忘记了告诉编译器也将它们隐式地设置为静态(至少从2.4.4开始)。

Unlike Java where enums are implicitly static, it appears that enums in Groovy are not implicitly static, hence why static imports don't work. This is because enums in Groovy aren't actually the same as the ones in Java, they made enhancements. Unfortunately it seems they have forgotten to tell the compiler to also make them implicitly static (at least as of 2.4.4).

我的建议是明确声明它们为静态( (如果可以的话),因为它与Groovy概念保持一致,即有效的Java是有效的Groovy。

My suggestion is to explicitly declare them static (if you can) as it would be keeping with the Groovy notion that valid Java is valid Groovy.

这篇关于在Groovy脚本中导入内部枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆