[英]method does not override or implement a method from a supertype @override compile error
[英]method does not override or implement a method from a supertype error
我正在嘗試為我的國際象棋游戲設計一種撤消/重做機制。.我決定使用將在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<Move> extends ArrayList<Move>
那是使用Move
作為泛型類型參數 ,而實際上您根本不需要泛型-您只想使用Move
作為ArrayList<E>
的類型參數 。 你要:
public class UndoStack extends ArrayList<Move>
那應該可以解決問題-盡管我個人強烈建議在這里使用組合而不是繼承。 (換句話說,讓您的UndoStack
類型包含 ArrayList<Move>
-或類似的名稱-而不是對其進行子類化。)
此外,這永遠行不通:
UndoStack undoStack;
private UndoStack() {
undoStack = new UndoStack();
}
這意味着要創建一個UndoStack
,您需要創建另一個UndoStack
……您希望這種情況如何發生? 當前,您將獲得一個堆棧溢出異常...為什么根本需要該變量?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.