从抽象中引用方法 class

Referencing method from abstract class

指令开始

如果点击的方格上有棋子(clickedSquare.getPiece()!= null) 确保游戏块属于当前玩家。您可以通过在 getPiece() 返回的 AbstractGamePiece 上调用 getPlayerType() 方法来获取拥有玩家。然后,您可以将其与 currentPlayerTurn JailBreak class 成员进行比较。

如果点击方格上的棋子属于当前玩家 将 selected 方块设置为等于单击的方块 在 selected 方块上调用 select() 方法以显示黄色边框

说明结束

我已经想出了如何通过实施 select() 方法来突出显示该作品,但是 我尝试了几种不同的实现方式,例如 AbstractGamePiece.getPlayerType()==currentPlayerTurn、使用嵌套的 if 语句在 clickedSquare.getPiece() 上设置条件,以及其他一些我想不出的实现方式。我似乎无法从摘要 class 中获得对 getPlayerType() 的引用。 class 中有预先编写的代码,就访问 AbstractGamePiece 而言似乎工作正常,例如

private void changePlayerTurn()
    {
        if (currentPlayerTurn == AbstractGamePiece.PLAYER_OUTLAWS)
            currentPlayerTurn = AbstractGamePiece.PLAYER_POSSE;
        else
            currentPlayerTurn = AbstractGamePiece.PLAYER_OUTLAWS;
    }

我觉得我做错了,但我似乎无法获得对 getPlayerType() 的引用。有一次,我创建了一个新的抽象对象 class,但是构造函数需要 3 个参数,这在这里不太合适。

支持代码:

abstract public class AbstractGamePiece
{
    // All class members are provided as part of the activity starter!
    
    // These two constants define the Outlaws and Posse teams
    static public final int PLAYER_OUTLAWS = 0;
    static public final int PLAYER_POSSE = 1;

    // These variables hold the piece's column and row index
    protected int myCol;
    protected int myRow;
    
    // This variable indicates which team the piece belongs to
    protected int myPlayerType;
     
    // These two strings contain the piece's full name and first letter abbreviation
    private String myAbbreviation;
    private String myName;

    // All derived classes will need to implement this method
    abstract public boolean hasEscaped();
    
    // The student should complete this constructor by initializing the member
    // variables with the provided data.
    public AbstractGamePiece(String name, String abbreviation, int playerType)
    {
    myName = name;
    myAbbreviation = abbreviation;
    myPlayerType = playerType;
    }

    public int getPlayerType()
    {
        return myPlayerType;    
    }
    public void setPosition (int row, int col)
    {
     myRow = row;
     myCol = col;
    }
    public int getRow()
    {
        return myRow;
    }
    public int getCol()
    {
        return myCol;
    }
    public String getAbbreviation() 
    {
        return myAbbreviation;
    }
    public String toString()
    {
        return (myName + " at " + "(" + myRow + "," + myCol + ")");
        
    }
    public boolean canMoveToLocation(List<GameSquare> path)
    {
        return false;
    }
    public boolean isCaptured(GameBoard gameBoard)
    {
        return false;
    }

乱码高亮代码实现成功

private void handleClickedSquare(GameSquare clickedSquare)
    {
        if (selectedSquare == null)
        {
            selectedSquare=clickedSquare;
            selectedSquare.select();
        }
        else if (selectedSquare == clickedSquare)
        {
        selectedSquare.deselect();
        selectedSquare = null;
        }
            
        else 
        {
        }

为什么我无法创建对 getPlayerType() 方法的引用?

只需对 X 类型的任何表达式调用 getPlayerType,其中 X 是 AbstractGamePiece 或其任何子类。例如,square.getPiece().getPlayerType().

方法引用是 java 中的内容,绝对不是您想要的,您使用的词语(“我无法创建对 getPlayerType 的引用”)有其他含义。方法引用看起来像 AbstractGamePiece::getPlayerType,并让您将调用该方法的概念传递给其他代码(例如,您可以制作一个连续调用某些代码 10 次的方法 - 所以此方法采用,如参数,'some code' - 方法引用是一种方式)。这不是您想要的,您只想调用该方法。这是通过 ref.getPlayerType() 完成的,其中 ref 是一个类型与 AbstractGamePiece 兼容的表达式。从上下文来看,clickedSquare.getPiece() 是那个表达式。