方法不覆盖抽象超类

method not overriding from abstract superclass

my class KTree 正在扩展抽象 class 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
}

KTreeclassaddEdge(E e, V v, V v1)中抛出错误

'addEdge(E, V, V)' in 'KTree' clashes with 'addEdge(E, V, V)' in 'GraphClass';both methods have the same erasure, yet neither overrides the other

KTree 中的 @override 抛出错误

Method does not override from its superclass


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

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

应该是:

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

问题是因为 KTree 中定义的 EVGraphClass 中定义的不同。基本上,您继承了 GraphClass 的原始形式。将 KTree 的声明更改为:

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

它应该可以工作。