【问题标题】:Is it possible to access variable of subclass using object of superclass in polymorphism是否可以在多态性中使用超类的对象访问子类的变量
【发布时间】:2011-02-05 07:13:44
【问题描述】:

如何使用KalaPlayer 类的对象访问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);
        }
    }
}

【问题讨论】:

    标签: polymorphism subclass superclass


    【解决方案1】:

    这是不可能的。

    但你可以走这条线:

    public KalaGameState state;

    并将其放在类KalaPlayer 的定义中。 这假定KalaPlayer 的每个子类都应该有自己的状态。

    您仍然可以在构造函数中为您正在创建的每个实例设置状态。

    所以这更像是一个设计问题而不是语言问题,因为您的超类应该包含所有子类共有的所有信息。

    【讨论】:

    • 非常感谢。很抱歉,但我是新手,这往往会让人感到困惑。
    • 继承是一种分层的东西,每个子类向存储在超类中的信息添加更多信息。因此,不可能访问未在类本身中指定的数据。通过将变量声明为 KalaPlayer,您可以告诉编译器只有来自 KalaPlayer 的信息可用。
    • 很高兴从任何反对者那里得到解释。
    【解决方案2】:

    你不能。

    没有能力从其父类访问子类的成员。如果有,以下情况会导致大规模混乱:

    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();
    

    【讨论】:

    • 我有点困惑多态是如何工作的。我有超类 KAlaPLayer 和子类键盘,每个键盘播放器实例都需要一个 KalaGAmeState 实例。如果我做多态然后 KalaPlayer player1=new KeyboardPLayer(); kalaPLayer.state*****whwre state 是 kalagamestae 的实例,在键盘播放器的构造函数中调用。请任何人帮忙。
    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多