请查看下面从 Eclipse Java IDE 截取的屏幕截图:
为什么 Eclipse 不建议导入 BigInteger
来自java.math.BigInteger
? BigInteger
的导入建议仅在导入 java.util.List
后出现.
有人可以解释一下这背后的原因吗?
更新:
为了检查以下答案是否是原因,我尝试了这个。我创建了一个自定义 class Test<T extends MyClass>
并检查了以下代码的建议。屏幕截图如下所示:
显然,java.math.BigInteger
不是有效的导入候选者。 Eclipse 仍然建议这样做。这证实了在之前的情况下,Eclipse 没有保留 BigInteger
的导入建议。过滤要导入的有效类。
最佳答案
我认为这背后的主要原因是 Eclipse 不知道 List
的类签名(因为缺乏更好的词语)是什么。由于您尚未导入 java.util.List
,因此 List
可以是任何内容。例如,您可以在另一个包中拥有自己的 List
类,声明为:
public class List<T extends Double> { ... }
或者类似的东西。
在这种情况下,我认为 Eclipse 不会想要猜测哪个类是有效的类型参数,直到它知道您要导入哪个 List
。
解决您的更新:
如果有的话,您的更新似乎证明了用户 Susannah Potts 在 their comment 中所说的话:
It sounds like an aspect of the parser that is used by Eclipse. It sees
List
not existing so it flags that first and doesn't check the type because you couldn't create the object you want to attribute the type to.
您还指出:
Obviously,
java.math.BigInteger
is not a valid candidate for import. Eclipse still suggests it. This confirms that in earlier case, Eclipse is not holding the import suggestion forBigInteger
to filter valid classes for import.
您的更新并未完全证实您的建议。当有效类不存在时,Eclipse 可以设计为列出与给定名称匹配的所有类。例如,唯一的 BigInteger
类是 java.math.BigInteger
,也许如果您有自己的自定义 BigInteger extends MyClass
,位于构建路径,那么 Eclipse 只会建议 BigInteger
类,或者在 java.math.BigInteger
之前建议它。
真正的答案:
基本上任何事情都可能发生,甚至可能有 Eclipse 查看的最常用类的缓存。 Eclipse 也有多个版本,每个版本都可能表现出不同的行为。获得有效答案的唯一方法是联系您正在使用的 Eclipse 版本的创建者并询问他们。
关于java - 缺少通用类类型的导入建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880838/