简体   繁体   English

是否允许在java枚举构造函数中使用varargs?

[英]Are varargs allowed in a java enum constructor?

enum MyEnum {
     A( 1, 2, 3, 4),
     B(1, 2),
     C(4, 5, 8, 8, 9);

    private MyEnum( int firstInt, int... otherInts ) {
     // do something with arguments, perhaps initialize a List
    }
}

Are there any problems with this? 这有什么问题吗? Any reasons not to do it? 有什么理由不去做吗?

Sure, this is perfectly legal. 当然,这是完全合法的。 No reason not to do it if your program requires it. 如果您的程序需要,没有理由不这样做。

it does work. 它确实有效。 You should try to 你应该尝试

private MyEnum(int... Ints )

With enums you need to make sure that you access them in a manner the initializes them. 使用枚举,您需要确保以初始化它们的方式访问它们。 A lot of the time an access is all that is needed 很多时候访问都是需要的

MyEnum bob = MyEnum.A;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM