【问题标题】:method does not override or implement a method from a supertype error方法不会覆盖或实现超类型错误中的方法
【发布时间】:2013-01-01 18:57:28
【问题描述】:

我正在尝试为我的国际象棋游戏设计一种撤消/重做机制。我决定使用将构建在 ArrayList 上的堆栈数据结构。我还希望我的 UndoStack 和 RedoStack 类应该是单例的。 . 但是我得到了

method does not override or implement a method from a supertype

pop() in UndoStack cannot implement pop() in IStackable
  return type Move is not compatible with cgas5.Move
  where Move is a type-variable:
    Move extends Object declared in class UndoStack

错误..

这是我的 IStackable 接口:

package cgas5;


public interface IStackable {

    abstract public Move pop();

    abstract public void push(Move m);

}

还有我的 UndoStack 类

package cgas5;

import java.util.ArrayList;

public class UndoStack<Move> extends ArrayList<Move> implements IStackable {

    UndoStack undoStack;

    private UndoStack() {
        undoStack = new UndoStack();
    }

    public UndoStack getUndoStack() {
        if (undoStack == null) {
            undoStack = new UndoStack();
        }
        return undoStack;
    }

    @Override
    public Move pop() {
        Move m = get(size() - 1);
        remove(size() - 1);
        return m;

    }

    @Override
    public void push(Move m) {
        add(m);
    }
}

如果有必要,我的 Move 类:

package cgas5;

public class Move {
    private Piece pieceToMove;
    private Square currentSquare;
    private Square targetSquare;
    private Piece capturedPiece;
    private Piece promotedPiece;

    public Move(){

    } 

    public Move(Piece pieceToMove, Square currentSquare, Square targetSquare){
        this.pieceToMove = pieceToMove;
        this.currentSquare = currentSquare;
        this.targetSquare = targetSquare;
    }

    public Piece getPieceToMove() {
        return pieceToMove;
    }

    public void setPieceToMove(Piece pieceToMove) {
        this.pieceToMove = pieceToMove;
    }

    public Square getCurrentSquare() {
        return currentSquare;
    }

    public void setCurrentSquare(Square currentSquare) {
        this.currentSquare = currentSquare;
    }

    public Square getTargetSquare() {
        return targetSquare;
    }

    public void setTargetSquare(Square targetSquare) {
        this.targetSquare = targetSquare;
    }

    public Piece getCapturedPiece() {
        return capturedPiece;
    }

    public void setCapturedPiece(Piece capturedPiece) {
        this.capturedPiece = capturedPiece;
    }

    public Piece getPromotedPiece() {
        return promotedPiece;
    }

    public void setPromotedPiece(Piece promotedPiece) {
        this.promotedPiece = promotedPiece;
    }

}

提前谢谢..

【问题讨论】:

  • 我认为这没有道理:public class UndoStack&lt;Move&gt;
  • @OliCharlesworth:就编译器而言,这是有道理的,但这不是 OP 想要的......
  • 在接口方法声明中,“abstract”和“public”都可以省略

标签: java interface singleton stack abstract


【解决方案1】:

这就是问题所在:

public class UndoStack<Move> extends ArrayList<Move> 

这是使用Move 作为泛型类型参数,而实际上你根本不想要泛型类型——你只想使用Move 作为类型参数 代表ArrayList&lt;E&gt;。你想要:

public class UndoStack extends ArrayList<Move> 

这应该可以解决问题 - 尽管我个人强烈建议在这里使用组合而不是继承。 (换句话说,让你的UndoStack 类型包含 ArrayList&lt;Move&gt; - 或类似的东西 - 而不是继承它。)

此外,这永远不会起作用:

UndoStack undoStack;

private UndoStack() {
    undoStack = new UndoStack();
}

这意味着要创建一个UndoStack,您需要创建另一个UndoStack...您希望这会如何发生?你现在会得到一个堆栈溢出异常......为什么你需要这个变量?

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2021-11-26
    • 1970-01-01
    相关资源
    最近更新 更多