简体   繁体   中英

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 .

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 . Every action that you need to do on mainCharacter will need to be defined in MainCharacter , not in the subclasses.

Second, you need to declare mainCharacter outside of the switch , then define it in the 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.

eg setup MainCharacter class like this:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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