枚举在Java数组常量 [英] Enums defined with Array Constant in java

查看:282
本文介绍了枚举在Java数组常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能与数组作为一个常量定义枚举;看到下面的code节选。

这并不与前pression错误的非法启动编译。可谁能帮助我?

非常感谢

 公开枚举货币
{
    ***** PENNY(1,{一,oneone})*
    镍(5,{十二五})
    DIME(10,{十})
    四分之一(25,{twentifive}); ****    私人诠释的立即;
    私有String []的valueString;    私人货币(INT的立即,字符串[]的valueString){
        this.valueInteger =的立即;
        this.valueString =的valueString;
    }}


解决方案

是的,你只需要对它们进行初始化。例如,

 镍(5,新的String [] {十二五})

I'd like to know if it is possible to define an enum with an Array as a constant; see the following code excerpt.

This does not compile with an illegal start of expression error. May anyone help me?.

Many thanks

public enum Currency
{
    *****PENNY(1, {"one", "oneone"}),*
    NICKLE(5, {"five"}),
    DIME(10, {"ten"}),
    QUARTER(25, {"twentifive"});****

    private int valueInteger;
    private String[] valueString;

    private Currency(int valueInteger, String[] valueString) {
        this.valueInteger = valueInteger;
        this.valueString = valueString;
    }

}

解决方案

Yes, you'll just need to initialize them. For example,

NICKLE(5, new string[]{"five"})

这篇关于枚举在Java数组常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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