gpt4 book ai didi

java - Eclipse 调用层次结构不适用于接口(interface)实现

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:53 25 4
gpt4 key购买 nike

当代码将类实例化为其实现的接口(interface)类型并调用接口(interface)方法时,该方法调用不会显示在方法实现(具体类)的调用层次结构中。

这是故意的、错误还是配置问题?

最佳答案

...the method call isn't shown in the call hierarchy of the method implementation (concrete class).

这是因为具体的类可能直到运行时才知道。

这是一个在调用 add() 时已知具体类的示例;它不可能是 ArrayList 之外的任何东西:

final Collection<Integer> coll = new ArrayList<Integer>();
coll.add(3);

在该示例中,Eclipse 可以(但不会)在 add() 的调用层次结构中显示 ArrayList;相反,它显示集合

但是这个例子怎么样?..

int rand = new Random().nextInt(99);
Collection<Integer> coll2 = (rand < 50) ? new ArrayList<Integer>() : new ArrayDeque<Integer>();
coll2.add(5);

在这种情况下,当 Eclipse 显示 add() 的调用层次结构时,它无法知 Prop 体类是 ArrayList 还是 ArrayDeque,但无论哪种方式,它都确定接口(interface)类型是 Collection,因此这就是调用层次结构中显示的内容。

Is this intentional, a bug or a matter of configuration?

好吧,您必须询问 Eclipse 团队对此的最终决定,但在我看来这是有意为之并且工作正常。

这当然不是一个错误,据我所知,如果在编译时确定已知,则无法更改配置以用具体类替换接口(interface)类型。 (我认为这可能是可能的,但在我看来这不是一个好主意;它只会造成困惑。)

关于java - Eclipse 调用层次结构不适用于接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49294015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com