gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:54:06 25 4
gpt4 key购买 nike

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

所以目前在我正在做的一个项目中,我必须设置一个值对象,忽略它是一个 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。是的,这就是《龙与地下城》。所以这些值已经在另一个类中设置了。我不打算粘贴整个类(class)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 值,然后在每个类上读取技能类中每个字符串的能力值并设置技能通过从能力中检索指定的字符串来获取值。例如。在读取 acrobaticsAbility 等于 DEX 后,它会检索 Dexterity 值。

最佳答案

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

您还可以将能力键放入枚举中,例如能力类型,这提高了可读性并减少了可能的拼写错误。那么静态字符串将是静态的 SkillType,因此也将是 map 中键的类型。

关于java - 基于其他值设置对象的更优雅的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44504093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com