Java 使用组合

Java using composition

我开始开发一款基本的 RPG 游戏,但遇到了一些小问题。我有一个角色 class,这是我们的英雄及其数据。我正在使用构图来创建角色。我遇到的问题是在createCharacter方法中的CharacterCreatorhero有问题。为什么,我该如何解决这个问题?这是代码:

public class Game {
    public static void main(String[] args) throws IOException {

        Character h = CharacterCreator.createCharacter();

        try {
            h.displayCharacter();
        } catch (Exception e) {
            System.out.println("Wrong");
        }

    }

}

public class CharacterCreator {

    public static Character createCharacter() {
        System.out.println("Choose a character: ");
        System.out.println("1. Fighter");
        System.out.println("2. Rogue");
        System.out.println("3. Mage");
        System.out.println("4. Cleric");

        Scanner sc = new Scanner(System.in);
        int scan = sc.nextInt();
        String chosenClass = getCharacterClass(scan);

        System.out.println("Choose Name:");
        Scanner nameIn = new Scanner(System.in);
        String name = nameIn.next();

        Character hero = null;

        switch (chosenClass) {
        case "Fighter":
            Fighter hero = new Fighter(name);
            break;
        case "Rogue":
            Rogue hero = new Rogue(name);
            break;
        case "Mage":
            Mage hero = new Mage(name);
            break;
        case "Cleric":
            Cleric hero = new Cleric(name);
            break;
        }

        return hero;
    }

    public static String getCharacterClass(int scan) {

        String classIn;

        switch (scan) {
        case 1:
            classIn = "Fighter";
            break;
        case 2:
            classIn = "Rogue";
            break;
        case 3:
            classIn = "Mage";
            break;
        case 4:
            classIn = "Cleric";
            break;
        default:
            System.out.println("Enter again");
            classIn = "def";
        }

        return classIn;
    }

}

public class Character {

    private String name;
    private String characterClass;
    private int level;
    private int hp;
    private int currentHp;
    private int armorClass;

    private long xp;
    /* private int BAB; /*Base attack bonus */

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

    protected Character(String name) {

        setName(name);
        characterClass = "Class";
        setLevel(1);
        setStrength(10);
        setConstitution(14);
        setDexterity(14);
        setIntelligence(10);
        setWisdom(10);
        setCharisma(10);
        setHp(0 + getModifier(getConstitution()));
        setCurrentHp(getHp());
        setArmorClass(10 + getModifier(getDexterity()));
        setXp(0);

    }

    void displayCharacter() throws IOException {
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        System.out.println("Name: " + getName());
        System.out.println("Class: " + getCharacterClass());
        System.out.println("Level: " + getLevel());
        System.out.println("HP: " + getHp());
        System.out.println("Armor Class: " + getArmorClass());

        System.out.println("***************");
        System.out.println("Attributes: ");
        System.out.println("Strength: " + getStrength());
        System.out.println("Constitution: " + getConstitution());
        System.out.println("Dexterity: " + getDexterity());
        System.out.println("Intelligence: " + getIntelligence());
        System.out.println("Wisdom: " + getWisdom());
        System.out.println("Charisma: " + getCharisma());
        System.out.println("***************");
        System.out.println("XP: " + getXp());

    }

    public int getModifier(int number) {
        int mod = (int) ((number - 10) / 2);
        return mod;
    }

    public String getName() {
        return name;
    }

    public String getCharacterClass() {
        return characterClass;
    }

    public int getLevel() {
        return level;
    }

    public int getHp() {
        return hp;
    }

    public int getCurrentHp() {
        return currentHp;
    }

    public int getArmorClass() {
        return armorClass;
    }

    public int getStrength() {
        return strength;
    }

    public int getConstitution() {
        return constitution;
    }

    public int getDexterity() {
        return dexterity;
    }

    public int getIntelligence() {
        return intelligence;
    }

    public int getWisdom() {
        return wisdom;
    }

    public int getCharisma() {
        return charisma;
    }

    public long getXp() {
        return xp;
    }

    protected void setName(String Name) {
        name = Name;
    }

    protected void setCharacterClass(String characterClass) {
        this.characterClass = characterClass;
    }

    protected void setLevel(int lvl) {
        level = lvl;
    }

    protected void setHp(int hitPoints) {
        hp = hitPoints;
    }

    protected void setCurrentHp(int curHp) {
        currentHp = curHp;
    }

    protected void setArmorClass(int ac) {
        armorClass = ac;
    }

    protected void setStrength(int str) {
        strength = str;
    }

    protected void setConstitution(int con) {
        constitution = con;
    }

    protected void setDexterity(int dex) {
        dexterity = dex;
    }

    protected void setIntelligence(int intel) {
        intelligence = intel;
    }

    protected void setWisdom(int wis) {
        wisdom = wis;
    }

    protected void setCharisma(int cha) {
        charisma = cha;
    }

    protected void setXp(int XP) {
        xp = XP;
    }

}

class Fighter extends CharacterClass {

    Fighter(String name) {

        Character hero = new Character(name);

        hero.setName(name);
        hero.setCharacterClass("Fighter");
        hero.setLevel(1);
        hero.setStrength(14);
        hero.setConstitution(16);
        hero.setDexterity(14);
        hero.setIntelligence(10);
        hero.setWisdom(10);
        hero.setCharisma(10);
        hero.setHp(10 + hero.getModifier(getConstitution()));
        hero.setCurrentHp(hero.getHp());
        hero.setArmorClass(10 + hero.getModifier(getDexterity()));
    }

}

我发现您的程序中存在潜在的编译器错误。这是罪魁祸首:

switch (chosenClass){
        case "Fighter":
            Fighter hero = new Fighter(name);
            break;
        case "Rogue":
            Rogue hero = new Rogue(name);
            break;
        case "Mage":
            Mage hero = new Mage(name);
            break;
        case "Cleric":
            Cleric hero = new Cleric(name);
            break;
    }

在 switch case 之外声明一次 hero 变量。

编辑:

createCharacter 方法检查这段修改后的代码。

Character hero = null;

switch (chosenClass){
    case "Fighter":
        hero = new Fighter(name);
        break;
    case "Rogue":
        hero = new Rogue(name);
        break;
    case "Mage":
        hero = new Mage(name);
        break;
    case "Cleric":
        hero = new Cleric(name);
        break;
}

这是你主要应该做的。执行此操作后,您会看到更多错误出现。这是因为,您已将 createCharacter 方法声明为 return Character 实例,但 Mage,Cleric, Fighter,Rogue 都是 CharacterClass 的子 class。我在您的代码中没有看到名为 CharacterClass 的 class。也许这就是 Character - 您拥有的 class。因此,我建议更改以上 4 个子 classes 以从 CharacterClass class 的 Character istead 扩展,我在您的代码中看不到它存在。

最后,根据需要初始化,从基础 class 构造函数调用超级 class 构造函数。

您正在 switch case 中创建新的英雄实例并将它们分配给局部变量。您在进入 switch 语句之前创建的 hero 变量将始终保持为空。您需要将您的新英雄分配给这个变量。

Character hero = null;
switch (chosenClass){
    case "Fighter":
        hero = new Fighter(name);
        break;
    case "Rogue":
        hero = new Rogue(name);
        break;
    case "Mage":
        hero = new Mage(name);
        break;
    case "Cleric":
        hero = new Cleric(name);
        break;
}
return hero;