2010-04-07 2 views
3

클래스의 객체를 사용하여 KeyBoardPlayer 클래스의 상태 변수에 어떻게 액세스 할 수 있습니까?다형성에서 수퍼 클래스의 객체를 사용하여 서브 클래스의 변수에 접근 할 수 있습니까?

/** 
    * 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 = new KeyboardPLayer(); kalaPLayer.state ***** 상태는 keyboardplayer의 생성자에서 호출되는 kalagamestae의 인스턴스입니다. pls 누구든지 도움을 줄 수 있습니다. – fari

-2

할 수 없다.

하지만 당신은 라인을 수행 할 수 있습니다

public KalaGameState state;

및 클래스 KalaPlayer의 정의에 넣어. 여기에는 KalaPlayer의 각 하위 클래스가 자체 상태 여야한다고 가정합니다.

그런 다음 생성중인 각 인스턴스에 대해 생성자에서 상태를 설정할 수 있습니다.

슈퍼 클래스에는 모든 하위 클래스에 공통적 인 모든 정보가 포함되어 있기 때문에 이것은 언어 문제보다 설계상의 문제입니다.

+0

Thnak 정말 고마워. 미안하지만이 일에 익숙하지 않고 혼란스러워하는 경향이 있습니다. – fari

+0

상속은 각 하위 클래스가 수퍼 클래스에 저장된 정보에 더 많은 정보를 추가하는 계층적인 것입니다. 따라서 클래스 자체에 지정되지 않은 데이터는 액세스 할 수 없습니다. 변수를 KalaPlayer로 선언하면 컴파일러에서 KalaPlayer의 정보 만 사용할 수 있다고 알립니다. – MKroehnert

+0

downvoters에서 설명을 얻는 것이 좋을 것입니다. – MKroehnert

관련 문제