2010-04-07 84 views
3

如何訪問類的狀態變量和類KalaPlayer的對象?是否可以使用多態的超類對象訪問子類的變量

/** 
    * An abstract class representing a player in Kala. Extend this class 
    * to make your own players (e.g. human players entering moves at the keyboard 
    * or computer players with programmed strategies for making moves). 
    */ 
public abstract class KalaPlayer { 

    /** 
     * Method by which a player selects a move. 
     * @param gs The current game state 
     * @return A side pit number in the range 1-6 
     * @throws NoMoveAvailableException if all side pits for the player are empty 
     * (i.e. the game is over) 
     */ 
    public abstract int chooseMove(KalaGameState gs) throws NoMoveAvailableException; 

} 



public class KeyBoardPlayer extends KalaPlayer { 
    /** 
    * Method by which a player selects a move. 
    * @param gs The current game state 
    * @return A side pit number in the range 1-6 
    * @throws NoMoveAvailableException if all side pits for the player are empty 
    * (i.e. the game is over) 
    */ 
    public KalaGameState state; 

    public KeyBoardPlayer() { 
     System.out.println("Enter the number of stones to play with: "); 
     try { 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      int key = Integer.parseInt(br.readLine()); 
      state=new KalaGameState(key); 
      //key=player1.state.turn; 
     } catch(IOException e) { 
      System.out.println(e); 
     } 
    } 

    public int chooseMove(KalaGameState gs) throws NoMoveAvailableException{ 
     return 0; 
    } 
} 

import java.io.IOException; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 

public class KalaGame { 
    KalaPlayer player1,player2; 

    public KalaGame(KeyBoardPlayer Player1,KeyBoardPlayer Player2) { 
     //super(0); 
     player1=new KeyBoardPlayer(); 
     player2 = new KeyBoardPlayer(); 
     //player1=Player1; 
     //player2=Player2; 
     //player1.state ****how can i access the stae variable from Keyboard CLass using object from KalaPlayer 
     key=player1.state.turn; 
    } 

    public void play() { 
     System.out.println("Enter the number of stones to play with: "); 
     try { 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
      int key = Integer.parseInt(br.readLine()); 
      System.out.println(key); 
      KalaGameState state=new KalaGameState(key); 
      printGame(); 
     } catch(IOException e) { 
      System.out.println(e); 
     } 
    } 
} 

回答

6

你不能。

沒有能力從父級訪問子類的成員。如果有,下面將導致亂傳:

public class Vehicle 
{ 

} 

public class Car extends Vehicle 
{ 
    SteeringWheel _wheel = new SteeringWheel(); 

    public SteeringWheel getSteeringWheel { return _wheel; } 

    public SteeringWheel setSteeringWheel(SteeringWheel value) 
    { 
     _wheel = value; 
    } 
} 

public class Bicycle extends Vehicle 
{ 

} 

然後:

Vehicle myVehicle = new Bicycle(); 

// This call couldn't possibly work since Bicylce has no steering wheel 
// and Vehicle isn't aware of what the type of the is/isn't. 
SteeringWheel myWheel = myVehicle.getSteeringWheel(); 
+0

我有點困惑多態如何工作。我有超類KAlaPLayer和子類鍵盤,每個keyboardplayer實例都需要有一個KalaGAmeState實例。如果我做多態然後 KalaPlayer player1 = kalaPLayer.state ***** whwre狀態是在keyboardplayer的構造函數中調用的喀拉ame族的實例。 任何人都可以幫忙。 – fari 2010-04-07 14:40:33

-2

這是不可能的事情。

但你可以採取行:

public KalaGameState state;

,並把它放在類KalaPlayer的定義。 這假設每個KalaPlayer的子類都應該有自己的狀態。

然後,您仍然可以在您創建的每個實例的構造函數中設置狀態。

所以這是一個設計問題,而不是語言問題,因爲你的超類應該包含所有子類通用的所有信息。

+0

Thnak你非常感謝。我很抱歉,但我對此很陌生,而且往往會感到困惑。 – fari 2010-04-07 14:51:04

+0

繼承是一種層次結構,每個子類都向超類中存儲的信息添加更多信息。 因此,訪問類中沒有指定的數據是不可能的。 通過將變量聲明爲KalaPlayer,您告訴編譯器只有來自KalaPlayer的信息可用。 – MKroehnert 2010-04-07 14:55:16

+0

從任何一個downvoters那裏得到解釋是很好的。 – MKroehnert 2017-01-18 22:15:03

相關問題