简体   繁体   English

如何使用内部静态最终字段初始化Java枚举?

[英]How to initialise a Java enum using an inner static final field?

I am designing a text-only videogame with two characters not often seen together, yet very much alike in heart and disposition. 我正在设计一个纯文本视频游戏,其中包含两个不常见的角色,但在内心和性格方面非常相似。

My problem is that I don't know how to initialise an enum constant through a constructor using a static final inner constant. 我的问题是我不知道如何使用静态最终内部常量通过构造函数初始化enum常量。 Otherwise the game is good to go. 否则游戏很不错。 ;) ;)

Here's the dilemma: 这就是困境:

  1. The enum constants must be defined in the first line of the enum, if I am not mistaken 如果我没有弄错的话,枚举常量必须在枚举的第一行中定义
  2. The first line can't refer to anything coming after it (ie "cannot reference a field before it is defined") 第一行不能引用它之后的任何内容(即“在定义之前无法引用字段”)

How do I resolve this catch-22? 我如何解决这个问题-22?

Here some sample code released from the game under non-disclosure agreement: 这里根据保密协议从游戏中发布了一些示例代码:

enum ValiantHeroWithPrincessSavingTendencies {

  SUPERMARIO(TYPICAL_QUOTE_FROM_MARIO), ZELDA(TYPICAL_QUOTE_FROM_ZELDA);

  private String aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;

  public String getQuoteUnderStressfulCircumstances() {
    return aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;
  }

  private ValiantHeroWithPrincessSavingTendencies(String quote) {
    aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive = quote;
  }

  private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
  private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
}

I am trying to initialise SUPERMARIO using TYPICAL_QUOTE_FROM_MARIO but I haven't defined TYPICAL_QUOTE_FROM_MARIO yet. 我正在尝试使用TYPICAL_QUOTE_FROM_MARIO初始化SUPERMARIO,但我还没有定义TYPICAL_QUOTE_FROM_MARIO。 Moving the private static final field before SUPERMARIO is illegal, I think. 我想,在SUPERMARIO之前移动私有静态最终字段是非法的。

The only viable options are to either a) move your constants to another class or b) just put your constants directly into the value initializers. 唯一可行的选择是:a)将常量移动到另一个类或b)将常量直接放入值初始值设定项中。

If you move your constants, you can make the class a static class in the enum: 如果移动常量,可以使类成为枚举中的静态类:

enum ValiantHeroWithPrincessSavingTendencies {
  SUPERMARIO(Quotes.TYPICAL_QUOTE_FROM_MARIO),
  ZELDA(Quotes.TYPICAL_QUOTE_FROM_ZELDA);

  private String aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;

  public String getQuoteUnderStressfulCircumstances() {
    return aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive;
  }

  private ValiantHeroWithPrincessSavingTendencies(String quote) {
    aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive = quote;
  }

  private static class Quotes {
    private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
    private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
  }
}

You can just access them via class name: 您只需通过类名访问它们:

enum ValiantHeroWithPrincessSavingTendencies {
    SUPERMARIO(ValiantHeroWithPrincessSavingTendencies.TYPICAL_QUOTE_FROM_MARIO),
    ZELDA(ValiantHeroWithPrincessSavingTendencies.TYPICAL_QUOTE_FROM_ZELDA);

    ...

    private static final String TYPICAL_QUOTE_FROM_ZELDA = "Have at ya!";
    private static final String TYPICAL_QUOTE_FROM_MARIO = "We, wagliu'!";
}

It's simplier than Brian's solution 它比Brian的解决方案更简单

The private static final constants are local to the enum; 私有静态最终常量是枚举的本地常量; just code them in the instance definitions. 只需在实例定义中对它们进行编码。 After that point they can be accessed internally from the aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive variable. 在那之后,可以从aPreparedQuotePurportedToBeSpontaneousAlmostImpulsive变量内部访问它们。

You could always do something hacky like this: 你总是可以像这样做一些hacky:

public enum Derp
{
    SOMETHING(),
    SOMETHINGELSE();

    private String herp;

    public static final String A = "derp", B = "derp2";

    public String getHerp()
    {
        return herp;
    }

    static
    {
        SOMETHING.herp = A;
        SOMETHINGELSE.herp = B;
    }
}

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

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