[英]method does not override or implement a method from a supertype error
I'm trying to design an undo/redo mechanism to my Chess game.. I decided to use stack data structure which is going to build on an ArrayList.. I also want that my UndoStack and RedoStack classes should be singleton.. However i'm getting 我正在尝试为我的国际象棋游戏设计一种撤消/重做机制。.我决定使用将在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
error.. 错误..
Here is my IStackable interface: 这是我的IStackable接口:
package cgas5;
public interface IStackable {
abstract public Move pop();
abstract public void push(Move m);
}
and my UndoStack class 和我的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);
}
}
and if it's necessary my Move class: 并且如果需要我的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;
}
}
Thanks in advance.. 提前致谢..
This is the problem: 这就是问题:
public class UndoStack<Move> extends ArrayList<Move>
That's using Move
as a generic type parameter , whereas really you don't want a generic type at all - you just want to use Move
as the type argument for ArrayList<E>
. 那是使用
Move
作为泛型类型参数 ,而实际上您根本不需要泛型-您只想使用Move
作为ArrayList<E>
的类型参数 。 You want: 你要:
public class UndoStack extends ArrayList<Move>
That should fix the problem - although personally I'd strongly recommend using composition instead of inheritance here. 那应该可以解决问题-尽管我个人强烈建议在这里使用组合而不是继承。 (In other words, make your
UndoStack
type contain an ArrayList<Move>
- or something similar - rather than subclassing it.) (换句话说,让您的
UndoStack
类型包含 ArrayList<Move>
-或类似的名称-而不是对其进行子类化。)
Additionally, this is never going to work: 此外,这永远行不通:
UndoStack undoStack;
private UndoStack() {
undoStack = new UndoStack();
}
That means that to create an UndoStack
, you need to create another UndoStack
... how do you expect that to happen? 这意味着要创建一个
UndoStack
,您需要创建另一个UndoStack
……您希望这种情况如何发生? You'll currently get a stack overflow exception... why do you need the variable at all? 当前,您将获得一个堆栈溢出异常...为什么根本需要该变量?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.