-5

对于我的程序,我有大量的公共静态 int,它主要是一个可读性的东西,所以我可以做一些事情,比如SkillsArray[Archery] = 25一眼就能看到“射箭技能”的值/级别/等级为 25,而不是试图弄清楚应该是什么SkillsArray[7] = 25意思。

它基本上是命名要在关系数据库结构中使用的唯一 ID,但如果我想打印这些名称,我会构建一个函数,该函数接受一个 int 参数,该参数读取一个巨大的 if/else 树以吐出一个名为“射箭”或“烹饪”之类的。

我想用简单的东西替换所有这些功能

String printStaticName() {
    return this.getClass().getName();
}

它可以读取附加到它的原语并获得它的名称,但 Eclipse 告诉我我不能对原语类型执行这些操作。想法?

我目前如何使用我拥有的系统的一个例子。

//races
public static final int Chosen = 0;
public static final int Native = 1;
public static final int High_Elf = 2;
public static final int Wood_Elf = 3;
public static final int Gremlin = 4;
public static final int Goblin = 5;
public static final int Hobgoblin = 6;
    ....

String getRaceNameString(int r){
    String temp = "No Race Set";
    if (r == Chosen){temp = "Chosen";};
    if (r == Native){temp = "Native";};
    if (r == High_Elf){temp = "High Elf";};
    if (r == Wood_Elf){temp = "Wood Elf";};
    if (r == Gremlin){temp = "Gremlin";};
    if (r == Goblin){temp = "Goblin";};
    if (r == Hobgoblin){temp = "Hobgoblin";};
    return temp;
}//close getRaceNameString

int getRaceIDFromString(String r){
    int result = 0;
    if (r == "Chosen"){result = Chosen;};
    if (r == "Native"){result = Native;};
    if (r == "High Elf"){result = High_Elf;};
    if (r == "Wood Elf"){result = Wood_Elf;};
    if (r == "Gremlin"){result = Gremlin;};
    if (r == "Goblin"){result = Goblin;};
    if (r == "Hobgoblin"){result = Hobgoblin;};
    return result;
}//close getraceIDFromString()

    class Race extends WoSCharacter {
private int raceID = Chosen;
private double purity = 1;
private String active_headshot = "headshot.jpg";
private String active_fullbody = "TemplateforPortrait.png";
private String Description;
private double minHeight;
private double maxHeight;
private int AttributeMods[] = new int[9];
private double lifespan;
private boolean isModRace = false;

void setRace(int RName, long pure, int gender){
    purity = pure;
    raceID = RName;
    switch (raceID){
    //region case
    case Chosen:
        AttributeMods[STR] = 5;
        AttributeMods[DEX] = 5;
        AttributeMods[CON] = 5;
        AttributeMods[CHA] = 5;
        AttributeMods[PER] = 5;
        AttributeMods[WIL] = 5;
        AttributeMods[INT] = 5;
        AttributeMods[INS] = 5;
        AttributeMods[WIS] = 5;
        active_headshot = "headshot.jpg";
        Description = "The race of man are the sole survivors of the Mage Wars that took destroyed magic and everything but the animal races and humanity. Now they are facing their own extinction as the Black Plague washes over the world, crossing continents and destroying entire family lines. Some of mankind have developed an immunity, being christened 'the chosen' but this this comes at cost as they are further divorced from the just now returning powers of magic.";
        minHeight = 150;
        maxHeight = 170;
        lifespan = 50;
        if (gender == MALE){
            active_headshot = "male_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 162;
            maxHeight = 190;
            lifespan = 49;
        }else if (gender == FEMALE){
            active_headshot = "Fem_headshot.jpg";
            active_fullbody = "Fantasy_Landscape_01.jpg";
            minHeight = 150;
            maxHeight = 170;
            lifespan = 43;
        }//gender headshot switch
        break;
        //endregion case
    case Native:
        AttributeMods[STR] = 5;
        AttributeMods[DEX] = 5;
        AttributeMods[CON] = 5;
        AttributeMods[CHA] = 5;
        AttributeMods[PER] = 5;
        AttributeMods[WIL] = 5;
        AttributeMods[INT] = 5;
        AttributeMods[INS] = 5;
        AttributeMods[WIS] = 5;
        active_headshot = "headshot.jpg";
        Description = "The race of man are the sole survivors of the Mage Wars that took destroyed magic and everything but the animal races and humanity. Now they are facing their own extinction as the Black Plague washes over the world, crossing continents and destroying entire family lines. Some of mankind have developed an immunity, being christened 'the chosen' but this this comes at cost as they are further divorced from the just now returning powers of magic.";
        minHeight = 150;
        maxHeight = 170;
        lifespan = 49;
        if (gender == MALE){
            active_headshot = "male_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 162;
            maxHeight = 190;
            lifespan = 49;
        }else if (gender == FEMALE){
            active_headshot = "Fem_headshot.jpg";
            active_fullbody = "TemplateforPortrait.png";
            minHeight = 150;
            maxHeight = 170;
            lifespan = 43;
        }//gender headshot switch
        break;
      }

    }
4

2 回答 2

2

改用枚举(前提是您的技能是固定的):

public enum Skill
{
    ARCHERY,
    ETC
}

为了构建技能“阵列”,请使用EnumMap. 例如,您可以在枚举中执行以下操作:

public enum Skill
{
    ARCHERY,
    ETC;

    public static Map<Skill, Integer> newSkillMap()
    {
        final Map<Skill, Integer> ret = new EnumMap(Skill.class);
        for (final Skill skill: values())
            ret.put(skill, 0);
        return ret;
    }
}

用法:

final Map<Skill, Integer> skillMap = Skill.newSkillMap();
map.get(Skill.ARCHERY); // skill for archery
map.put(Skill.ARCHERY, 30); // change value

您甚至可以改进您的枚举,以获得默认值等;枚举可以在 Java中做很多事情。样本:

public enum Skill
{
    ARCHERY(10, "archery"),
    ETC(0, "etc");

    private final int defaultValue;
    private final String description;

    Skill(final int defaultValue, final String description)
    {
        this.defaultValue = defaultValue;
        this.description = description;
    }

    public static Map<Skill, Integer> newSkillMap()
    {
        final Map<Skill, Integer> ret = new EnumMap(Skill.class);
        for (final Skill skill: values())
            ret.put(skill, skill.defaultValue);
        return ret;
    }

    @Override
    public String toString()
    {
        return description;
    }
}
于 2013-06-16T17:01:53.390 回答
0

您可以将枚举用于您的目的:

public enum Skill {
  SWORD(0, "Sword"),
  ARCHERY(1, "Archery")
  ;

  public final String name;
  public final int index;

  Skill(int index, String name) {
    this.name = name;
    this.index = index;
  }
}

skillsArray[ARCHERY.index] = 25;
System.out.println(ARCHERY.name+" is +"skillsArchery[ARCHERY.index]);

请注意,ARCHERY.ordinal()如果您更喜欢分配给您的enum值的内置索引,则可以使用。这将更安全但不太灵活(因为它将取决于枚举声明中强加的位置)。

于 2013-06-16T17:04:08.630 回答