如何在 Eclipse 中使用“推断通用类型参数...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13641725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use "Infer Generic Type Arguments..." in Eclipse
提问by Michael Wiles
Whenever generics are missing from source code in eclipse it suggests I "Infer Generic Type Arguments..."
每当 eclipse 中的源代码中缺少泛型时,它会建议我“推断泛型类型参数...”
The problem is that I don't think "Infer Generic Type Arguments..." has ever actually inferred anything. It typically comes up with no suggestions.
问题是我认为“推断通用类型参数...”实际上并没有推断出任何东西。它通常不会提出任何建议。
What scenarios does it work for? How does it work?
它适用于哪些场景?它是如何工作的?
There have been a few cases where it is possible to "infer" something - eclipse still comes up blank.
在某些情况下,可以“推断”某些东西 - eclipse 仍然是空白的。
采纳答案by tuergeist
From Eclipse Help:
Replaces raw type occurrences of generic types by parameterized types after identifying all places where this replacement is possible.
Available: Projects, packages, and types
Options: 'Assume clone() returns an instance of the receiver type'. Well-behaved classes generally respect this rule, but if you know that your code violates it, uncheck the box.
在确定可以进行此替换的所有位置后,将泛型类型的原始类型替换为参数化类型。
可用:项目、包和类型
选项:'假设 clone() 返回接收器类型的实例'。行为良好的类通常遵守此规则,但如果您知道您的代码违反了该规则,请取消选中该框。
Leave unconstrained type arguments raw (rather than inferring ). If there are no constraints on the elements of e.g. ArrayList a, uncheck this box will cause Eclipse to still provide a wildcard parameter, replacing the reference with ArrayList.
保留不受约束的类型参数原始(而不是推断)。如果对例如 ArrayList a 的元素没有约束,取消选中此框将导致 Eclipse 仍然提供通配符参数,用 ArrayList 替换引用。
You can find an example at the end of the page.
HTH
HTH
回答by Brian
Here's an example showing how to use "Infer Generic Type Arguments" in eclipse:
这是一个示例,展示了如何在 Eclipse 中使用“推断通用类型参数”:
First declare a generic class
首先声明一个泛型类
// GenericFoo.java
public class GenericFoo<T> {
private T foo;
public void setFoo(T foo) {
this.foo = foo;
}
public T getFoo() {
return foo;
}
}
Then instantiate it without specifying the type, and do an unnecessary type casting.
然后在不指定类型的情况下实例化它,并进行不必要的类型转换。
// GenericFooUsage.java before refactoring
public class GenericFooUsage {
public GenericFooUsage() {
GenericFoo foo1 = new GenericFoo<Boolean>();
foo1.setFoo(new Boolean(true));
Boolean b = (Boolean)foo1.getFoo();
}
}
After applying "Infer Generic Type Arguments", the code is refactored as:
应用“Infer Generic Type Arguments”后,代码重构为:
// GenericFooUsage.java after refactoring
public class GenericFooUsage {
public GenericFooUsage() {
GenericFoo<Boolean> foo1 = new GenericFoo<Boolean>();
foo1.setFoo(new Boolean(true));
Boolean b = foo1.getFoo();
}
}
So what "Infer Generic Type Arguments" does are :
那么“推断通用类型参数”的作用是:
- automatically infer the type of generic arguments.
- remove unnecessary type casting.
- 自动推断泛型参数的类型。
- 删除不必要的类型转换。