【问题标题】:Type E recognized as an Object类型 E 被识别为对象
【发布时间】:2018-05-22 09:10:19
【问题描述】:

为什么要写这个:

Node<Vertex<E>, Double> a = queue.extractMax();
Vertex<E> u = a.getValue();

代码编译没有错误,如果我这样写:

Vertex<E> u = queue.extractMax().getValue();

我得到了错误:

错误:不兼容的类型:对象无法转换为顶点

【问题讨论】:

  • 哪个Java版本,编译器和编译器版本?
  • extractMax()的签名是什么?
  • 您可能有不同的签名。如果你这样做Vertex&lt;E&gt; u = ((Node&lt;Vertex&lt;E&gt;, Double&gt;) queue.extractMax()).getValue();,它会起作用
  • Java 9. 公共节点 extractMax()
  • 如果他的签名不同于Node, Double> a = queue.extractMax();没有演员也不会工作

标签: java methods generic-programming


【解决方案1】:

假设您的 extractMax() 签名/返回类型不同于 Node&lt;Vertex&lt;E&gt;, Double&gt; -

Vertex<E> u = ((Node<Vertex<E>, Double>) queue.extractMax()).getValue();

这个绝对应该按照你的例子工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2014-12-31
    • 2021-02-07
    • 2011-08-24
    • 2021-04-01
    • 2020-09-16
    • 2011-09-13
    相关资源
    最近更新 更多