方法没有从抽象超类

时间:2015-10-06 02:03:23

标签: java generics inheritance override abstract-class

我的班级KTree正在扩展抽象类GraphClass,但GraphClass中定义的方法之一无法覆盖。

public abstract class GraphClass<V extends VertexInterface<?> ,E extends EdgeInterFace <V,?>> implements UndirectedGraph<V,E>{
//other methods

    @Override
    public boolean addEdge(E e, V v, V v1) {
        return false;
    }

//more methods
}

原始方法addEdge(E e, V v, V v1)在界面UndirectedGraph<V,E>

中定义
public class KTree<V extends VertexInterface<?> ,E extends EdgeInterFace <V,?>> extends GraphClass {
//other methods

    @Override
    public boolean addEdge(E e, V v, V v1) {

        if(!this.Nodes.contains(v) || !this.Nodes.contains(v1) || this.Edges.containsValue(e)){
            return false;
        }

        v.setDegree(v.getDegree()+1);
        v1.setDegree(v1.getDegree()+1);

        this.Edges.put(e.getHashCode(),e);

        return true;
    }

//more methods
}
KTree类中的

addEdge(E e, V v, V v1)引发错误

  'KTree'中的'addEdge(E,V,V)'与'addEdge(E,V,V)'冲突   'GraphClass';两种方法都有相同的擦除,但都没有覆盖   另一个

KTree中的@override会引发错误

  

方法不会覆盖其超类

我理解为什么他们有相同类型的擦除但我认为如果我添加了@override,它将默认为所有KTree实例的该方法版本。我要做的只是从GraphClass覆盖该方法,但不知道为什么它不能识别@override。这是否与覆盖已经覆盖接口方法的方法有关?

2 个答案:

答案 0 :(得分:3)

问题是因为E中定义的VKTreeGraphClass中定义的GraphClassKTree不同。基本上,您继承了public class KTree<V extends VertexInterface<?>, E extends EdgeInterFace <V, ?>> extends GraphClass<V, E> 的原始形式。将您 private boolean checkVerticalWin() { PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; System.out.println("check vert"); for(int j = 0; j < myNumColumns; j++) { for(int i = 0; i < myNumRows; i++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; if(count == 1) { myWinBegin = new Point(i,j); } } else { myWinBegin = null; count = 0; } System.out.println(count); if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } myWinBegin = null; return false; } private boolean checkHorizontalWin() { System.out.println("test"); PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; for(int i = 0; i < myNumRows; i++) { for(int j = 0; j < myNumColumns; j++) { if(myBoard[i][j] == type && myBoard[i][j] != null) { count++; if (count == 1) { myWinBegin = new Point(i,j); } } else { myWinBegin = null; count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } myWinBegin = null; return false; } private boolean checkDiagonalWin() { PieceType type = myBoard[myLastPoint.x][myLastPoint.y]; for(int i = 0; i < myNumRows; i++) { for (int j = 0; j < myNumColumns; j++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; myWinEnd = new Point(i,j); } if(count == myWinLength) { return true; } } } for(int j = 0; j < myNumColumns; j--) { for (int i = 0; i < myNumRows; i--) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } for(int j = 0; j < myNumColumns; j++) { for (int i = 0; i < myNumRows; i--) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; } else { myWinBegin = new Point(i,j); count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } for(int j = 0; j < myNumColumns; j--) { for (int i = 0; i < myNumRows; i++) { if(myBoard[i][j] == type && myBoard[i][j] != null ) { count++; myWinBegin = new Point(i,j); } else { count = 0; } if(count == myWinLength) { myWinEnd = new Point(i,j); return true; } } } return false; } 的声明更改为:

while

它应该有用。

答案 1 :(得分:2)

我认为这是因为您在GraphClass声明中以原始格式(无类型参数)使用了KTree

应该是:

public class KTree<V extends VertexInterface<?> ,E extends EdgeInterFace <V,?>> extends GraphClass<V, E>