简体   繁体   English

方法不会从超类型错误中覆盖或实现方法

[英]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.

相关问题 方法不会从父类型@override覆盖或实现方法编译错误 - method does not override or implement a method from a supertype @override compile error 错误:(124,9)错误:方法未覆盖或从超类型实现方法 - Error:(124, 9) error: method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型实现方法 - error: method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型OnCreateOptionsMenu实现方法 - error: method does not override or implement a method from a supertype OnCreateOptionsMenu Java“方法没有覆盖或实现来自超类型的方法”错误 - Java "method does not override or implement a method from a supertype" error React Native - 错误:方法不会覆盖或实现超类型的方法 - React Native - error: method does not override or implement a method from a supertype Java错误:方法未从超类型重写或实现方法 - java error: method does not override or implement a method from a supertype 错误:方法onEnable不会覆盖或实现超类型的方法 - Error: method onEnable does not override or implement a method from a supertype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM