基于其他值设置对象的更优雅方式

时间:2017-06-12 16:13:27

标签: java object

首先对本标题的状态道歉,我不知道我实际问的问题是什么,这意味着我不知道使用的正确术语,如果有人能告诉我我的话很乐意改变标题或删除并重新发布。

所以目前在我正在进行的一个项目中,我必须设置一个值的对象,忽略它是一个无所谓的realmObject这一事实。

public class Skills extends RealmObject {

    private boolean acrobaticsMarked = false;
    private int acrobaticsValue;
    private final String acrobaticsAbility = "DEX";

    private boolean animalHealingMarked = false;
    private int animalHealingValue;
    private final String animalHealingAbility = "WIS";

    private boolean arcanaMarked = false;
    private int arcanaValue;
    private final String arcanaAbility = "INT";

    private boolean athleticsMarked = false;
    private int athleticsValue;
    private final String athleticsAbility = "STR";

    private boolean deceptionMarked = false;
    private int deceptionValue;
    private final String deceptionAbility = "CHA";

    private boolean historyMarked = false;
    private int historyValue;
    private final String historyAbility = "INT";

    private boolean insightMaarked = false;
    private int insightValue;
    private final String insightAbility = "WIS";

    private boolean intimidationMarked = false;
    private int intimidationValue;
    private final String intimidationAbility = "CHA";

    private boolean investigationMarked = false;
    private int investigationValue;
    private final String investigationAbility = "INT";

    private boolean medicineMarked = false;
    private int medicineValue;
    private final String medicineAbility = "WIS";

    private boolean natureMarked = false;
    private int natureValue;
    private final String natureAbility = "INT";

    private boolean perceptionMarked = false;
    private int perceptionValue;
    private final String perceptionAbility = "WIS";

    private boolean performanceMarked = false;
    private int performanceValue;
    private final String performanceAbility = "CHA";

    private boolean persuasionMarked = false;
    private int persuasionValue;
    private final String persuasionAbility = "CHA";

    private boolean religionMarked = false;
    private int religionValue;
    private final String religionAbility = "INT";

    private boolean sleightOfHandMarked = false;
    private int sleightOfHandValue;
    private final String sleightOfHandAbility = "DEX";

    private boolean stealthMarked = false;
    private int stealthValue;
    private final String stealthAbility = "DEX";

    private boolean survivalMarked = false;
    private int survivalValue;
    private final String survivalAbility = "WIS";

正如您所看到的,每个人都有能力值 STR,DEX,CON,INT,WIS,CHA。是的它是D& D. 所以这些值已在另一个类中设置。我不打算粘贴整个班级you can go find it here,但它具有以下值,此时已填写。

int strength;
int dexterity;
int constitution;
int intelligence;
int wisdom;
int charisma;

目前我正在设置像这样的值

public void setValues(Abilities abilities) {
    this.setAcrobaticsValue(abilities.getDexterity());
    this.setAnimalHealingValue(abilities.getWisdom());
    this.setArcanaValue(abilities.getIntelligence());
    this.setAthleticsValue(abilities.getStrength());
    this.setDeceptionValue(abilities.getCharisma());
    this.setHistoryValue(abilities.getIntelligence());
    this.setInsightValue(abilities.getWisdom());
    this.setIntimidationValue(abilities.getCharisma());
    this.setInvestigationValue(abilities.getIntelligence());
    this.setMedicineValue(abilities.getWisdom());
    this.setNatureValue(abilities.getIntelligence());
    this.setPerceptionValue(abilities.getWisdom());
    this.setPerformanceValue(abilities.getCharisma());
    this.setPersuasionValue(abilities.getCharisma());
    this.setReligionValue(abilities.getIntelligence());
    this.setSleightOfHandValue(abilities.getDexterity());
    this.setStealthValue(abilities.getDexterity());
    this.setSurvivalValue(abilities.getWisdom());
}

我想知道是否有一种更优雅的方式逐行运行这个类,所以它首先遍历每个类的int值,并在每个类中读取每个String的Ability的值。技能课程并通过从能力中检索所述的字符串来设置技能值。例如。在阅读acrobaticsAbility等于DEX后,它会去并检索Dexterity值。

4 个答案:

答案 0 :(得分:1)

你应该创建一个具有3个属性的类能力:标记,值和(静态)能力。此对象将替换每组3个属性。然后,您可以更改setValues以处理Ability对象的集合。也许你只需要一个普通的setter并保持这个集合。或者您可以使用地图,其中您将字符串功能作为键。通过这种方式,您可以使用该键找到所需的能力。

您还可以将能力键放入枚举中,例如AbilityType,这提高了可读性并减少了可能的拼写错误。然后静态字符串将是一个静态的AbilityType,因此将是地图中键的类型。

答案 1 :(得分:0)

我最好的选择是在技能和能力课程之间建立关系。

public class Skills extends RealmObject {
   // Remove all fields that can be obtained from ability
   private Ability ability;

   public int getAcrobaticsValue(){
   return ability.getWisdom();
 }
}

答案 2 :(得分:0)

我没有解决方案,所以这可能没有帮助;相反,我有一些建议。

技能似乎有几种技能"条目。您可以使用封装在那里的三个属性创建一个Skill类。

然后,您将获得具有多个技能条目的技能。相反,你可以通过" ability"来获得一个技能图,它可以是一个String常量(静态最终字符串)或一个枚举。

此时,您可以删除Skill类,并通过" ability"来获得整数Map。 "标记为"可以通过注意是否在地图中找到密钥来替换属性。但是,保留Skill类允许您向其添加其他属性。

就简化setValues()而言,这将涉及沿着相同的行重构能力,以便您可以遍历每个能力并添加技能。

答案 3 :(得分:0)

粗略结构请考虑以下因素:

    public class Abilities{

        enum Ability {DEXTERITY, WISDOM; }//add more

        private Map<Ability, Integer> abilities;

        public Abilities(){
            abilities = new HashMap<>();
        }

        public int getValue(Ability ability) {
            return abilities.get(ability);
        }

        public void setValue(Ability ability, int value) {
            abilities.put(ability, value);
        }
    } 

public class Skills {

    enum SkillName{DEX, WIS, INT, STR, CHA;}

    private Skill acrobatics;
    private Skill animalHealing;
    private Abilities abilities;

    Skills(){
        abilities = new Abilities();
        abilities.setValue(Ability.DEXTERITY, 5);
        abilities.setValue(Ability.WISDOM, 3);
        acrobatics    = new Skill(SkillName.DEX, Ability.DEXTERITY, false);
        animalHealing = new Skill(SkillName.WIS, Ability.WISDOM, false);
    }

    public boolean isFlag(Skill skill) {
        return skill.isFlag();
    }

    public void setFlag(Skill skill, boolean flag) {
        skill.setFlag(flag);
    }

    public int getValue(Skill skill) {
        return skill.getValue();
    }

    public void setValue(Skill skill, int value) {
        skill.setValue(value);
    }

    class Skill{

        private SkillName name;
        private Ability ability;
        boolean flag;

        Skill(SkillName name, Ability ability, Boolean flag){

            this.name = name;
            this.ability = ability;
            this.flag = flag;
        };

        public boolean isFlag() {return flag;}
        public void setFlag(boolean flag) { this.flag = flag;}
        public int getValue() { return abilities.getValue(ability);}
        public void setValue(int value) {abilities.setValue(ability,value);}
        public String getName() {return name.toString();}
        public void setName(SkillName name) {this.name = name;  }
    }
}