简体   繁体   English

如何编写基于用户输入的构造函数?

[英]how can i write a user input based constructor?

I want to have multiple classes and want to construct one based on user input. 我想要多个类,并希望根据用户输入构造一个类。 Specifically I am writing a text adventure and have class for each type of "player class" that the player can be. 具体来说,我正在编写文字冒险游戏,并为玩家可以使用的每种“玩家类别”提供了一个类别。 My three classes extend a parent "stats" class. 我的三个班级扩展了父级“统计”班级。

Here is a part of my code: (I am using a print constructor for writing efficiency) 这是我的代码的一部分:(我使用打印构造函数来提高书写效率)

switch (answer) {
        case 1:
            {
                adv.print("you are a mage");
                mainCharacterMage mainCharacter = new mainCharacterMage();
                break;
            }
        case 2:
            {
                adv.print("you are an assasin");
                mainCharacterAssasin mainCharacter = new mainCharacterAssasin();
                break;
            }
        case 3:
            {
                adv.print("you are a fighter");
                mainCharacterFighter mainCharacter = new mainCharacterFighter();
                break;
            }
        default:
            adv.print("error wrong answer");
            break;
    }
    String printThis = Integer.toString(mainCharacter.getHealth());
    adv.print("your health is "+printThis);

I'll assume the parent class of the three subclasses is called MainCharacter . 我假设这三个子类的父类称为MainCharacter

First, mainCharacter needs to be of type MainCharacter , unless you're willing to do instanceof checks and casts every time you want to use mainCharacter . 首先, mainCharacter必须为MainCharacter类型,除非您愿意在每次要使用mainCharacter时进行instanceof检查和强制转换。 Every action that you need to do on mainCharacter will need to be defined in MainCharacter , not in the subclasses. 每一个动作,你需要做上mainCharacter将需要加以界定MainCharacter ,而不是在子类。

Second, you need to declare mainCharacter outside of the switch , then define it in the switch : 其次,你需要声明mainCharacter的外部switch ,然后在把它定义switch

MainCharacter mainCharacter; // Declare it outside
switch (answer) {
        case 1:
            {
                adv.print("you are a mage");
                mainCharacter = new MainCharacterMage(); // Then define it on the inside
                break;
            }
        case 2:
            {
                adv.print("you are an assasin");
                mainCharacter = new MainCharacterAssasin();
                break;
            }
        case 3:
            {
                adv.print("you are a fighter");
                mainCharacter = new MainCharacterFighter();
                break;
            }
        default:
            adv.print("error wrong answer");
            break;
    }

May be something like this 可能是这样的

public interface Character {
  // here is all common method of your Character
}

public class CharacterFactory {
    private class CharacterMage implements Character {
       // here is implementation
    }

    private class CharacterAssasin implements Character {
       // here is implementation
    }

    public Character createCharacter(String characterName) {
         switch (characterName) {
             case "Mage": 
                 return new CharacterMage();

             case "Assasin":
                 return new CharacterAssasin();

             default:
                 throw new IllegalArgumentException("Incorrect character type " + characterName);
    }
}

Depending on what the differences between classes are, this could be done with only one MainCharacter class and different factory methods for each class. 根据类之间的差异,可以仅使用一个MainCharacter class以及每个类的不同工厂方法来完成此操作。

eg setup MainCharacter class like this: 例如,像这样设置MainCharacter类:

public class MainCharacter{
    public int health;
    public int damage;
    // etc.
    public static MainCharacter buildMage(){
        health = 5;
        damage = 20;
        // etc.
    }
    public static MainCharacter buildAssassin(){
        health = 10;
        damage = 10;
        // etc.
    }
    public static MainCharacter buildMage(){
        health = 20;
        damage = 5;
        // etc.
    }
}

then create the MainCharacter like this: 然后像这样创建MainCharacter:

switch (answer) {
    case 1:
        {
            adv.print("you are a mage");
            MainCharacter main_character = MainCharacter.buildMage();
            break;
        }
    case 2:
        {
            adv.print("you are an assasin");
            MainCharacter main_character = MainCharacter.buildAssassin();
            break;
        }
    case 3:
        {
            adv.print("you are a fighter");
            MainCharacter main_character = MainCharacter.buildFighter();
            break;
        }

NOTE: This reduces the number of classes you have to create, however it is only appropriate if the differences between classes are just different initial stats. 注意:这减少了必须创建的类的数量,但是仅当类之间的差异只是不同的初始统计信息时才合适。 If the different classes actually have inherently different methods, then inhertiance would be needed. 如果不同的类实际上具有本质上不同的方法,则需要继承。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM