return 子类时方法的不兼容 return 类型

Incompatible return types for method when returning subclass

我正在尝试定义一种方法 return 由签名

给出的图上给定顶点的所有邻居
public abstract class GraphClass<V extends Vertex<?>,E extends Edge<V,?>> implements UndirectedGraph<V,E>{
.
.
.
        public ArrayList<V> getNeighbors(V v) {...}
}

我希望从我的 KTree class 中覆盖这个方法,它扩展了上面的 GraphClass 如下

public class KTree extends GraphClass<KVertex,KEdge> {...
    public ArrayList<KVertex> getNeighbors(KVertex v) {
            return v.getAdjList();
    }
}

这给了我以下错误

Incompatible types. found 'java.ustil.ArrayList>', required 'java.ustil.ArrayList'


KVertex class 还扩展了原来的 Vertex class,其中找到了 .getAdjList() 方法

public class KVertex extends Vertex<Integer> {...}

 public class Vertex<V>{
        protected ArrayList<Vertex<V>> neighbours = new ArrayList<>();
        ...
        public ArrayList<Vertex<V>> getAdjList(){
            return neighbours;
        }
    }

我在编写此方法时的假设是 return 该类型的子 class 应该仍然是有效的 return 类型,因为 KVertex Inheriting Vertex class ,并保留 is-a 关系。我如何才能正确定义 KVertex class 或 getNeighbours 方法,以便我可以 return 顶点的任何子 class 的列表。谢谢!

嗯……我不太确定,但也许这会奏效……

public class Vertex<V>{
        protected ArrayList<Vertex<V>> neighbours = new ArrayList<Vertex<V>>();
        ...
        public ArrayList<Vertex<V>> getAdjList(){
            return neighbours;
        }
    }

主要问题出在顶点class方法

public ArrayList<Vertex<V>> getAdjList()
{
  return neighbours;
}

暗示它将 return 一个 ArrayList<Vertex<Integer>> 为您的 KVertex class。

但是getNeighbours(V v)想要return一个ArrayList<KVertex>,它与ArrayList<Vertex<Integer>>不协变,所以这不可能发生。 is-a 关系在 classes 之间有效,但在类型变量之间无效:a List<KVertex> is-not-a List<Vertex<Integer>>.

解决您的问题的方法是将 Vertex 的真实类型传递给 class 本身,例如:

  class Vertex<V, R extends Vertex<V, R>>
  {
    protected List<R> neighbours = new ArrayList<>();

    public List<R> getAdjList()
    {
      return neighbours;
    }
  }

  public abstract class GraphClass<V extends Vertex<?,?>,E extends Edge<V,?>> implements UndirectedGraph<V,E>
  {
    public abstract List<? extends V> getNeighbors(V v);
  }

  public class KVertex extends Vertex<Integer, KVertex>
  {

  }


  public class KTree extends GraphClass<KVertex,KEdge>
  {
    @Override
    public List<KVertex> getNeighbors(KVertex v)
    {
       return v.getAdjList();
    }
  }

通过这种方式,您可以使 getAdjList return 成为扩展 Vertex<V>.

类型的 List