【发布时间】:2015-10-23 12:12:57
【问题描述】:
我在编写我的第一个 java 程序(所以这个问题相对简单)。我开发了某种基本的角色扮演游戏,我研究角色的属性。
我的问题是:
- 如何从我创建的枚举中检索数据(问题出在 Character 构造函数中。我需要根据所选字符类从 ClassStats 获取值)?
- 能否以更好的方式存储每个字符类的属性初始值?
enum ClassStats {
Fighter(15,14,12,10,9,10), Rogue(12,12,16,14,10,10), Mage(10,10,14,16,14,10), Cleric(12,14,12,13,16,14);
private int strength, constitution, dexterity, intelligence, wisdom, charisma;
ClassStats(int str, int con, int dex, int intel, int wis, int cha){
strength = str;
constitution = con;
dexterity = dex;
intelligence = intel;
wisdom = wis;
charisma = cha;
}
int getStrength(){
return strength;
}
int getConstitution(){
return constitution;
}
int getDexterity(){
return dexterity;
}
int getIntelligence(){
return getIntelligence();
}
int getWisdom(){
return wisdom;
}
int getCharisma(){
return charisma;
}
}
public class Character {
private String Name;
private String Class;
private int Level;
private long XP;
private int HP;
private int currentHp;
/*private int BAB; /*Base attack bonus*/
private int Strength;
private int Constitution;
private int Dexterity;
private int Intelligence;
private int Wisdom;
private int Charisma;
Character(String name, String chracterClass){
Name = name;
Class = chracterClass;
Level = 1;
XP = 0;
HP = CharacterUtil.setHP(chracterClass);
currentHp = HP;
ClassStats cs = null;
Strength = cs.getStrength();
System.out.println("Strength: " + Strength);
Constitution = cs.getConstitution();
Dexterity = cs.getDexterity();
Intelligence = cs.getIntelligence();
Wisdom = cs.getWisdom();
Charisma = cs.getCharisma();
}
}
【问题讨论】:
-
请遵守 Java 编码约定:类型名称(类、接口、枚举)应以大写字母开头(例如
BigPicture)。方法、变量和字段名称应以小写字母开头(例如bigPicture),常量应全部大写(例如BIG_PICTURE)。此外,强烈建议避免使用与java.lang中的类相同的名称来命名您自己的类(例如Character)。