当前位置: 首页 > 知识库问答 >
问题:

类型块问题的未定义方法

子车英达
2023-03-14

我一直在做一个用Java编写国际象棋的项目。我有一个名为Piece的抽象类,它有一个抽象方法和两个普通方法。每个棋子我都有一个类,它们都扩展了棋子

有一个类也代表棋盘,叫做Board。在这个类中,我有一个方法来检查给定位置的棋子是否属于另一个玩家。在这个方法中,我调用了一个来自Piece类的方法,该方法获取名为getColor()的颜色。当我在一个棋子上调用这个方法时,它说“方法getColor()对于类型Piece是未定义的”,即使它是在Piece类中创建的方法。我真的不知道该怎么解决这个问题。

下面是Piece类代码:

abstract class Piece {
    private Color color;

    /**
     * @param color the color of the piece.
     */
    public Piece(Color color) {
        this.color = color;
    }

    /**
     * This method gives a list of all legal moves a piece at a given position in a
     * given board can do.
     * 
     * @param position the position of the piece in the board.
     * @param board    the board in which the piece is in.
     * @return a list of all legal moves.
     */
    abstract List<Position> getPossibleMoves(Position position, Board board);


    /**
     * @return the color of the piece.
     */
    public Color getColor() {
        return color;
    }

下面是课程代码:

public class Board {
    private Square[][] square;

    /**
     * Initialises an 8 by 8 squares 2d table.
     */
    public Board() {
        this.square = new Square[8][8];
        for (int row = 0; row < square.length; row++) {
            for (int column = 0; column < square.length; column++) {
                square[row][column] = new Square<>(null);
            }
        }
    } 

     /**
     * This method sets the given piece in the given position in the board.
     * Throw an exception if the position is not in the board.
     * 
     * @param piece    The piece to be set.
     * @param position The position to set the piece in.
     */
    public <Piece> void setPiece(Piece piece, Position position) {
        if (piece instanceof Piece) {
            if (contains(position)) {
                square[position.getRow()][position.getColumn()].setPiece(piece);
            } else {
                throw new IllegalArgumentException("The given position isn't part of the board :" + position);
            }
        }
    }

    /**
     * This method returns the piece that is in the given position in the board.
     * Throws an exception if the position is not in the board.
     * 
     * @param pos The position of the piece in the board.
     * @return the piece.
     */
    public <Piece> Piece getPiece(Position pos) {
        Piece piece;
        if (contains(pos)) {
            piece = (Piece) square[pos.getRow()][pos.getColumn()].getPiece();
        } else {
            throw new IllegalArgumentException("The given position isn't part of the board :" + pos);
        }
        return piece;
    }
     /**
     * This method determines whether or not the piece at the given position has the
     * same or the opposite color as the given color.
     * throws an exception if position is not in the board.
     * 
     * @param pos the position of the piece to be checked.
     * @param col the color to check if the piece is in the opposite color or not.
     * @return true if the piece is in the opposite color, false otherwise.
     */
    public <Piece> boolean containsOppositeColor(Position pos, Color col) {
        boolean containsOpposite = false;
        if (contains(pos)) {
            if (!isFree(pos)) {
                Piece piece = getPiece(pos);
                containsOpposite = (col.opposite() == piece.getColor());
            }
        } else {
            throw new IllegalArgumentException("The given position isn't part of the board :" + pos);
        }
        return containsOpposite;
    }

问题出在这样一行:containsPosite=(col.containst()==piece)。getColor())


共有1个答案

宗穆冉
2023-03-14

问题是您定义了一个泛型类型

比如说

public <Piece> Piece getPiece(Position pos)

应该是

public Piece getPiece(Position pos)

其他方法也是如此:

public <Piece> void setPiece(Piece piece, Position position)

应该是

public void setPiece(Piece piece, Position position)

public <Piece> boolean containsOppositeColor(Position pos, Color col)

应该是

public boolean containsOppositeColor(Position pos, Color col)

 类似资料:
  • 在下,出现一条红线,表示:TouchActions TapObjectsSue类型的方法tap(TapOptions)未定义

  • 颜色选取器 在 1.受保护的无效上创建(捆绑保存实例状态) { 超级.on创建(已保存实例状态); 错误:创建(捆绑包)上的方法未定义对象 类型2.new MyView(指画活动.this); 错误:构造函数指画活动.MyView(指画活动)未定义 3.set内容查看(mv); 错误:他的方法集内容视图(指画活动.MyView)是未定义的类型指画活动 4.公共布尔值在创建选项菜单(菜单菜单) {

  • 正如代码所示,我想知道的是,在内存分配中,隐藏Base而不调用子的额外方法的实际情况是什么,它被调用了什么,有没有一种通过Base调用方法的方法。请帮忙

  • 错误在最后一行,我想知道到底出了什么问题,以及如何修复它,谢谢!

  • 问题内容: 取自Android的想法:从对话框中模糊和暗化背景窗口。我无法使对话框下的内容模糊。当调用eula.getWindow()时,我收到此错误: 对于类型AlertDialog.Builder,未定义方法getWindow() 随同主要活动中的以下代码一起显示出eula: 任何帮助是极大的赞赏。 问题答案: 是对话框类的方法,而不是对话框生成器的方法。您的代码应该看起来像这样: 请注意,尽

  • 问题内容: 我已将Java动态Web项目导入Eclipse IDE(已在Eclipse IDE中实现并正常工作)。 运行项目时,我收到“ 类型未定义”错误。 我已经按照结构将每个文件复制到IDE中,并且工作已完成。 现在我只想知道为什么在导入项目时出现此错误。有人遇到过同样的问题吗?请让我知道我可能犯了什么错误。 问题答案: 当我在IntelliJ中使用的servlet-api版本与Tomcat8