泛型错误:不适用于参数
有人可以向我解释为什么下面的代码不起作用吗?
public class Test {
interface Strategy<T> {
void execute(T t);
}
public static class DefaultStrategy<T> implements Strategy<T> {
@Override
public void execute(T t) {}
}
public static class Client {
private Strategy<?> a;
public void setStrategy(Strategy<?> a) {
this.a = a;
}
private void run() {
a.execute("hello world");
}
}
public static void main(String[] args) {
Client client = new Client();
client.setStrategy(new DefaultStrategy<String>());
client.run();
}
}
我收到以下错误:
The method execute(capture#3-of ?) in the type Test.Strategy<capture#3-of ?>
is not applicable for the arguments (String)
我通过如下更改代码使其工作:
public class Test {
interface Strategy<T> {
void execute(T t);
}
public static class DefaultStrategy<T> implements Strategy<T> {
@Override
public void execute(T t) {}
}
public static class Client<T> {
private Strategy<T> a;
public void setStrategy(Strategy<T> a) {
this.a = a;
}
private void run(T t) {
a.execute(t);
}
}
public static void main(String[] args) {
Client<String> client = new Client<String>();
client.setStrategy(new DefaultStrategy<String>());
client.run("hello world");
}
}
但我想了解为什么原始方法不起作用。
Can someone explain to me why the following code does not work?
public class Test {
interface Strategy<T> {
void execute(T t);
}
public static class DefaultStrategy<T> implements Strategy<T> {
@Override
public void execute(T t) {}
}
public static class Client {
private Strategy<?> a;
public void setStrategy(Strategy<?> a) {
this.a = a;
}
private void run() {
a.execute("hello world");
}
}
public static void main(String[] args) {
Client client = new Client();
client.setStrategy(new DefaultStrategy<String>());
client.run();
}
}
I'm getting the following error:
The method execute(capture#3-of ?) in the type Test.Strategy<capture#3-of ?>
is not applicable for the arguments (String)
I've got it to work by altering code as follows:
public class Test {
interface Strategy<T> {
void execute(T t);
}
public static class DefaultStrategy<T> implements Strategy<T> {
@Override
public void execute(T t) {}
}
public static class Client<T> {
private Strategy<T> a;
public void setStrategy(Strategy<T> a) {
this.a = a;
}
private void run(T t) {
a.execute(t);
}
}
public static void main(String[] args) {
Client<String> client = new Client<String>();
client.setStrategy(new DefaultStrategy<String>());
client.run("hello world");
}
}
but I want to understand why the original approach did not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案很简单:不能使用未绑定的通配符。它只是意味着“未知物体”。
它不会向编译器提供任何信息。 “?”任何类型的意思,所以实际上它太通用了,没有任何意义。
看一下这里: http://java.sun.com/ docs/books/tutorial/extra/generics/wildcards.html
如前所述:
由于我们不知道 c 的元素类型代表什么,因此我们无法向其中添加对象。 add() 方法采用 E 类型的参数,即集合的元素类型。当实际类型参数为?时,它代表某种未知类型。我们传递给 add 的任何参数都必须是该未知类型的子类型。因为我们不知道那是什么类型,所以我们不能传递任何东西。唯一的例外是 null,它是每个类型的成员。
编辑:别担心,这是对 java 通配符的正常误解当你开始使用它们时。这就是有界通配符(例如
)存在的原因,否则通用通配符几乎毫无用处,因为编译器无法对其做出任何假设。
The answer is simple: the unbound wildcard cannot be used. It simply means "uknown object".
It doesn't give anything informative to compiler. "?" means of whatever type, so actually it is too generic to mean anything.
Take a look here: http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html
As stated:
Since we don't know what the element type of c stands for, we cannot add objects to it. The add() method takes arguments of type E, the element type of the collection. When the actual type parameter is ?, it stands for some unknown type. Any parameter we pass to add would have to be a subtype of this unknown type. Since we don't know what type that is, we cannot pass anything in. The sole exception is null, which is a member of every type.
EDIT: don't worry, this is a normal misunderstanding of java wildcard when you start using them. That's why bounded wildcards (eg.
<? extends Something>
) exist, otherwise generic wildcard would be almost useless since compiler cannot make any assumptions on it.这不起作用,因为您的类
Client
不是为特定的Strategy
(Strategy
) 编写的,而是在run( )
方法,您传递一个String
(这仅适用于Strategy
!)。仅当您将a
的类型和setStrategy()
的参数更改为Strategy
类型时,这才有效!That does not work because your class
Client
is written for no specificStrategy
(Strategy<?>
) but in therun()
method, you pass aString
(which is only correct forStrategy<String>
!). That would only work if you'd changea
's type andsetStrategy()
's parameter to the typeStrategy<String>
!这是因为这不是类型安全的操作。 “?”是一个通配符,意味着我不知道类型。它并不意味着“任何类型”。阅读此... http://java.sun.com/j2se /1.5/pdf/generics-tutorial.pdf
This is because this is not a type safe operation. "?" is a wildcard that means I do not know the type. It does not mean "any type". read this... http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf