如何显示我从子类设置的参数的输出?

问题描述 投票:0回答:1

我需要帮助访问我使用子类输入的变量。我在主类中为子类创建了一个对象,但是,我不知道如何访问我输入的输入并将它们显示在代码末尾。

public abstract class player {
    public abstract String name(String name);
    public abstract void race(int race);
    public abstract int[] getStatArray();
}

import java.util.Scanner;
public class childplayer extends player {
    Scanner sc = new Scanner(System.in);
    
    public String name(String name) {
        System.out.print("Enter your name: ");
        name = sc.nextLine();
        return name;
    }
    
    public void race(int race) {
        System.out.println("Race options: ");
        System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
        System.out.print("Enter character's race: ");
        race = sc.nextInt();
        if((race>5)||(race<1)) {
            while ((race>5)||(race<1)) {
                System.out.print("Enter character's race: ");
                race = sc.nextInt();
            }
        }System.out.println(" ");
    }
    
    public int[] getStatArray(){
        int [] stat = new int [3];
        int x = 0, y = 0, pts = 0;
        System.out.println("Enter character's stats.");
        while(y<3) {
            System.out.print("Enter value: ");
            x = sc.nextInt();
            y++;
            pts = pts + x;
        }
        int i = 0;
        if(pts>10) {
            System.out.println("Invalid input. Try again.");
            while(i<3) {
                System.out.print("Enter value: ");
                x = sc.nextInt();
                i++;
                pts = pts + x;
            }
        }else {
            stat[i] = x;
            i++;
        }
        
        return stat;
    }

}
java inheritance abstract-class display
1个回答
1
投票

如果您想保留这些值以供以后使用,那么您需要存储它们。最好的方法就是使用一个类变量,如下所示:

public class Childplayer extends Player {

    //Create class variables that store the values
    String name = "";
    int race = -1;
    int[] stat = new int [3];

然后我们只需修改你的方法来使用这些变量,例如:

public String name(String name) {

    //Process the name
    if (someCondition) {
        name = name +" Smith"
    }

    //Saved the passed in variable to the class variable `this.name`
    this.name = name;

    return this.name;
}

另一个例子:

public void race(int race) {
    //Saved the passed in variable to the class variable `this.race`
    this.race = race:
}

然后为了稍后获取信息,我们只需使用:

// Earlier in the code
Childplayer playerA = new Childplayer();

// Some code here
//...

// Later in the code we can get the values
String name = playerA.name;
int storedRaceFromEarlier = playerA.race;

我强烈建议使用构造函数方法来填充类数据。为了这个例子,我简化了代码和值检查:

//Note that Java naming conventions require that classes should always have a capital letter at the start, I have fixed this in my example
public abstract class Player {
    //Example getter abstract methods that must be implemented
    public abstract String getName();
    public abstract int getRace();
    public abstract int[] getStatArray();
}

// Note that Java naming conventions require that classes should always have a capital letter at the start, I have fixed this in my example
public class Childplayer extends Player {
    Scanner sc = new Scanner(System.in);

    //Create class variables that store the values
    String name = "";
    int race = -1;
    int[] stat = new int [3];

    // Constructor method with exactly the same name as the class "Childplayer"
    // This method should be responsible for creating the object and populating data
    Childplayer() {
        //Set name
        System.out.print("Enter your name: ");
        name(sc.nextLine());
        System.out.print("Name set as " + name + "\r\n");
        
        // Set race
        System.out.println("Race options: ");
        System.out.println("\t 1 - Human \n\t 2 - Elf \n\t 3 - Orc \n\t 4 - Angel \n\t 5 - Demon");
        int result = -1;

        while ((result > 5) || (result < 1)) {
            System.out.print("Enter character's race: ");
            result = sc.nextInt();
        }

        // Set the race with the abstract method
        race(result);
        System.out.print("Race set as " + race + "\r\n");
        
        System.out.println("Enter character's stats.");
        int i = 0;

        while (i < 3) {
            System.out.print("Enter stat value: ");
            //Save the stat to the class variable
            stat[i] = sc.nextInt();
            i++;
        }
    }

    // Abstract methods implemented to return the correct values
    public String getName() {
        return name;
    }

    public int getRace() {
        return race;
    }

    public int[] getStatArray() {
        return stat;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.