首页 > 解决方案 > 当我将 Enum 实例变量设置为作为选项包含在 Enum 类本身中的值时,它无法解析为类型。为什么?

问题描述

//Enum class

package model.cards;

public enum Rarity {
      BASIC, COMMON, RARE, EPIC, LEGENDARY}

//Different class

package model.cards.minions;
import model.cards.Rarity;

public class Icehowl {
    private int manaCost=9;
    private Rarity rarity=LEGENDARY; //error "LEGENDARY" can not be resolved to a variable
    private boolean attacked=true;
    private int maxHP=10;
}

标签: javaenumsinstance

解决方案


你的意思是

private Rarity rarity = Rarity.LEGENDARY;

枚举值LEGENDARY是类型中的常量Rarity


推荐阅读