第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用派生類創(chuàng)建對象數(shù)組

使用派生類創(chuàng)建對象數(shù)組

瀟瀟雨雨 2023-09-27 21:19:00
我正在嘗試在我正在編寫的程序中創(chuàng)建兩個玩家。問題是可以創(chuàng)建三種類型的玩家,但為了稍后引用它們,我希望將它們命名為玩家一和玩家二。三個類:“Human”、“Computer”和“Karen”是“Player”的派生類。我想知道如何創(chuàng)建一個數(shù)組 Player,然后我可以將player1 和player2 對象放入其中。這可以嗎?int playerType = 0;int highestScore = 0;Player[] player = new Player[2]; // This is the array of type Player, the master classScanner 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 語句根據(jù)我們所在的 for 循環(huán)的迭代來創(chuàng)建對象playerOne 或playerTwo (i)。不幸的是,java 似乎不會編譯它,因為同一個變量有多個聲明,即使只有其中一個聲明應(yīng)該運行。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 建議的解決方案如下,效果非常好。但是,我現(xiàn)在在調(diào)用以下方法時遇到問題:人類、計算機和凱倫。嘗試執(zhí)行時出現(xiàn)以下錯誤: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 Player1 error每個類中的方法如下所示:public class Karen extends Player {private String playerName = "Karen";public String getName(){  return playerName;}}
查看完整描述

1 回答

?
白板的微信

TA貢獻1883條經(jīng)驗 獲得超3個贊

感謝老程序員:

更改“人類玩家[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();

    }


}


查看完整回答
反對 回復(fù) 2023-09-27
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號