Java - 寻找更有效的方法来编写类方法

标签 java class performance

我正在创建一个自动创建玩家角色的程序。下面是我的 PlayerCharacter 类。我注意到我对不同的变量重复了许多操作。

public class PlayerCharacter {

    int strength, dexterity, constitution, intelligence, wisdom, charisma;
    int[] strRolls, dexRolls, conRolls, intRolls, wisRolls, charRolls;

    public void generateAbilityScoresMethod1() {

        strRolls = new int[3];
        dexRolls = new int[3];
        conRolls = new int[3];
        intRolls = new int[3];
        wisRolls = new int[3];
        charRolls = new int[3];

        for(int i = 0; i < 3; i++) {

            strRolls[i] = dice.Dice.D6.getNewRoll();
            strength += strRolls[i];

            dexRolls[i] = dice.Dice.D6.getNewRoll();
            dexterity += dexRolls[i];

            conRolls[i] = dice.Dice.D6.getNewRoll();
            constitution += conRolls[i];

            intRolls[i] = dice.Dice.D6.getNewRoll();
            intelligence += intRolls[i];

            wisRolls[i] = dice.Dice.D6.getNewRoll();
            wisdom += wisRolls[i];

            charRolls[i] = dice.Dice.D6.getNewRoll();
            charisma += charRolls[i];

        }


    }

    public int getStrength() {
        return strength;
    }

    public void printStrRolls() {
        System.out.println("Str: roll 1 = " + strRolls[0]);
        System.out.println("Str: roll 2 = " + strRolls[1]);
        System.out.println("Str: roll 3 = " + strRolls[2]);
    }

    public int getDexterity() {
        return dexterity;
    }

    public void printDexRolls() {
        System.out.println("Dex: roll 1 = " + dexRolls[0]);
        System.out.println("Dex: roll 2 = " + dexRolls[1]);
        System.out.println("Dex: roll 3 = " + dexRolls[2]);
    }

    public int getConsitution() {
        return constitution;
    }

    public void printConRolls() {
        System.out.println("Con: roll 1 = " + conRolls[0]);
        System.out.println("Con: roll 2 = " + conRolls[1]);
        System.out.println("Con: roll 3 = " + conRolls[2]);
    }

    public int getIntelligence() {
        return intelligence;
    }

    public void printIntRolls() {
        System.out.println("Int: roll 1 = " + intRolls[0]);
        System.out.println("Int: roll 2 = " + intRolls[1]);
        System.out.println("Int: roll 3 = " + intRolls[2]);
    }

    public int getWisdom() {
        return wisdom;
    }

    public void printWisRolls() {
        System.out.println("Wis: roll 1 = " + wisRolls[0]);
        System.out.println("Wis: roll 2 = " + wisRolls[1]);
        System.out.println("Wis: roll 3 = " + wisRolls[2]);
    }

    public int getCharisma() {
        return charisma;
    }

    public void printCharRolls() {
        System.out.println("Char: roll 1 = " + charRolls[0]);
        System.out.println("Char: roll 2 = " + charRolls[1]);
        System.out.println("Char: roll 3 = " + charRolls[2]);
    }

    public void printAbilities() {
        System.out.println("Str = " + getStrength());
        System.out.println("Dex = " + getDexterity());
        System.out.println("Con = " + getConsitution());
        System.out.println("Int = " + getIntelligence());
        System.out.println("Wis = " + getWisdom());
        System.out.println("Char = " + getCharisma());
    }

}

如何以更高效的方式完成相同的任务?

最佳答案

您可以声明一个类 Ability 并创建 strengthdexterity ... 实例。以下片段可能是一个开始:

class Ability {

    private final int[] rolls;
    private int value;

    public Ability(int dice) {
        rolls = new int[dice];
    }

    public int roll() {
        value = 0;
        for (int i = 0; i < rolls.length; i++) {
            rolls[i] = dice.Dice.D6.getNewRoll();
            value += rolls[i];
        }
        return value;
    }

    public int getValue() {
        return value;
    }

    public void printRolls() {
        // ... tbd ...
    }

}

您可以使用...

Ability strength;
strength = new Ability(3);
strength.roll(); // get new value
System.out.println(strength.getValue()); // e.g. print
strength.printRolls(); // e.g. print rolls

关于Java - 寻找更有效的方法来编写类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16252318/

相关文章:

java - Jackson 从子节点反序列化到字段?

使用 id 和类的 CSS 样式链接

java - 当 stackoverflow 错误没有指向我们的 java 代码时,如何处理它

java - Android 创建类/库以与服务器通信

java - 如何在 Scala 或 Java 的控制台输出中使用颜色?

java - 与 JavaMail 的连接

php - 帮助 PHP call_user_func 并将函数集成到类中?

c# - 在 c# 中使用 'class' 关键字作为/在方法参数中

arrays - 如何在 MATLAB 中按特定值递增数组中的某些元素

java - 使用 Multi-Tenancy 时的 Spring Boot 范围问题