枚举内的枚举

时间:2019-05-15 15:35:41

标签: c# methods enums

我用Java编写了这段代码。我有这两个枚举,我想知道是否有可能在C#中编写一个等效于UnitType的枚举,因为它继续给我带来错误。

public enum GenericUnitType {
    BASIC, 
    NORMAL,
    HERO;
}
public enum UnitType {

    CLOSE_BASIC(GenericUnitType.BASIC),
    DISTANCE_BASIC(GenericUnitType.BASIC),
    CLOSE_NORMAL(GenericUnitType.NORMAL),
    DISTANCE_NORMAL(GenericUnitType.NORMAL),
    HERO_CLOSE_FIGHTER(GenericUnitType.HERO),
    HERO_DISTANCE_FIGHTER(GenericUnitType.HERO);


    private GenericUnitType genericType;

    UnitType(final GenericUnitType genericType) {
        this.genericType = genericType;
    }

    public GenericUnitType getGenericUnitType() {
        return this.genericType;
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您可以得到的最接近的是class ...

public enum GenericUnitType {
    BASIC, 
    NORMAL,
    HERO;
}

public class UnitType
{
    public static GenericUnitType CLOSE_BASIC = new UnitType(GenericUnitType.BASIC);
    public static GenericUnitType DISTANCE_BASIC = new UnitType(GenericUnitType.BASIC),
    public static GenericUnitType CLOSE_NORMAL = new UnitType(GenericUnitType.NORMAL),
    public static GenericUnitType DISTANCE_NORMAL = new UnitType(GenericUnitType.NORMAL),
    public static GenericUnitType HERO_CLOSE_FIGHTER = new UnitType(GenericUnitType.HERO),
    public static GenericUnitType HERO_DISTANCE_FIGHTER = new UnitType(GenericUnitType.HERO);

    private GenericUnitType _unitType;

    public UnitType(GenericUnitType unitType)
    {
        _unitType = unitType;
    }

    public GenericUnitType UnitType => _unitType;
}

应该给您带来相同的效果。 C#不具有将enum像Java中可以处理的类那样的功能。

相关问题