【问题标题】:method not overriding from abstract superclass方法不覆盖抽象超类
【发布时间】:2016-01-02 19:36:36
【问题描述】:

我的类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&lt;V,E&gt;


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)' 中的'addEdge(E, V, V)' 冲突 'GraphClass';两种方法都有相同的擦除,但都没有覆盖 另一个

KTree 中的 @override 抛出错误

方法不会覆盖其超类


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

【问题讨论】:

    标签: java generics inheritance overriding abstract-class


    【解决方案1】:

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

    应该是:

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

    【讨论】:

      【解决方案2】:

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

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

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-18
        相关资源
        最近更新 更多