【问题标题】:Creating an array of objects with derived classes使用派生类创建对象数组
【发布时间】:2020-02-04 04:59:42
【问题描述】:

我正在尝试在我正在编写的程序中创建两个播放器。问题是可以创建三种类型的播放器,但为了稍后引用它们,我希望将它们命名为播放器一和播放器二。三个类:“Human”、“Computer”和“Karen”是“Player”的派生类。我想知道如何创建一个数组 Player ,然后我可以将 player1 和 player2 对象放入其中。这可能吗?

int playerType = 0;
int highestScore = 0;
Player[] player = new Player[2]; // This is the array of type Player, the master class

Scanner userInput = new Scanner (System.in);

for (int i = 1; i < 3; i++)
{
    System.out.println ("Choose a type of player:");
    System.out.println ("1: Human");
    System.out.println ("2: Computer (Threshold = 20)");
    System.out.println ("3. Computer (Custom)");
    System.out.print ("? : ");

    playerType = Integer.valueOf(userInput.nextLine()); // gets input choice from user


  switch (playerType)
    {
      case 1:
        Human player[i] = new Human();
      case 2:
        Computer player[i] = new Computer();
      case 3:
        Karen player[i] = new Karen();
    }

}

我尝试了另一种方法,使用 if 语句根据我们所处的 for 循环的迭代 (i) 来创建对象 playerOne 或 playerTwo。不幸的是,它看起来不像 java 会编译它,因为同一个变量有多个声明,即使只有一个声明应该运行。

if(playerNumber == 1){
  switch (playerType)
    {
      case 1:
        Human playerOne = new Human();
      case 2:
        Computer playerOne = new Computer();
      case 3:
        Karen playerOne = new Karen();
    }
}

if(playerNumber == 2){
  switch (playerType)
    {
      case 1:
        Human playerTwo = new Human();
      case 2:
        Computer playerTwo = new Computer();
      case 3:
        Karen playerTwo = new Karen();
    }
}

OldProgrammer 建议的解决方案如下,效果很好。但是,我现在在调用以下方法时遇到问题:Human、Computer 和 Karen。尝试执行时出现以下错误:

System.out.println(player[i].getName() + ", your turn");

错误消息:

Main.java:38: error: cannot find symbol
    System.out.println(player[i].getName() + ", your turn");
                                ^
symbol:   method getName()
location: class Player
1 error

每个类中的方法如下所示:

public class Karen extends Player {
private String playerName = "Karen";

public String getName(){
  return playerName;
}

}

【问题讨论】:

  • 更改“人类玩家[i] = new Human();”到“player[i] = new Human();”等
  • @OldProgrammer 好的,效果很好。现在,尽管从各个类调用方法,但我遇到了问题。例如,如果我执行 player[i].getName() 应该在以下位置调用该方法:Human、Computer 或 Karen。但是我收到以下错误:错误:找不到符号 System.out.println(player[i].getName() + ", your turn"); ^
  • @OldProgrammer 我在问题中添加了我所指的内容。应该更清楚。

标签: java arrays object derived-class derived


【解决方案1】:

感谢老程序员:

更改“人类玩家[i] = new Human();”到“player[i] = new Human();”等

了解派生类的重要性,我很高兴它是这样工作的。非常感谢您,我希望这可能对将来的某人有所帮助。这是我更新的代码以进行全面说明:

int highestScore = 0;
Player[] player = new Player[2]; 

Scanner userInput = new Scanner (System.in);

for (int i = 0; i < 2; i++)
{
    System.out.println ("Choose a type of player:");
    System.out.println ("1: Human");
    System.out.println ("2: Computer (Threshold = 20)");
    System.out.println ("3. Computer (Custom)");
    System.out.print ("? : ");

    playerType = Integer.valueOf(userInput.nextLine());

  switch (playerType)
    {
      case 1:
        player[i] = new Human();
      case 2:
        player[i] = new Computer();
      case 3:
        player[i] = new Karen();
    }

}

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 2013-08-30
    • 2015-08-10
    • 2011-07-20
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多