泛型错误:不适用于参数 [英] generics error: not applicable for the arguments

查看:116
本文介绍了泛型错误:不适用于参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么下面的代码不起作用吗?

  public class Test {

接口策略< T> {
void execute(T t);
}

public static class DefaultStrategy< T>实施Strategy< T> {
@Override
public void execute(T t){}
}

public static class Client {
private Strategy<?>一个;

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();
}
}

我收到以下错误:

 方法execute(capture#3-of?)的类型为Test.Strategy< capture#3-of?> 
不适用于参数(字符串)

我已经使用它更改代码如下:

  public class Test {

interface Strategy< T> {
void execute(T t);
}

public static class DefaultStrategy< T>实施Strategy< T> {
@Override
public void execute(T t){}



public static class Client< T> {
私人策略< T>一个;

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);
}
}

但我想了解为什么原始方法没有工作。

解决方案

答案很简单:无法使用未绑定的通配符。它只是意味着未知对象。



它并没有给编译器提供任何信息。 ?无论什么类型的手段,所以实际上它是过于通用的任何意思。



看看这里: http://java.sun.com/docs/books/tutorial/extra/generics/wildcards.html



如上所述:

 集合<?> c = new ArrayList< String>(); 
c.add(new Object()); //编译时间错误

由于我们不知道c的元素类型因为我们不能添加对象。 add()方法使用类型E的参数,集合的元素类型。当实际的类型参数是?时,它代表某种未知的类型。我们通过添加的任何参数都必须是此未知类型的子类型。由于我们不知道这是什么类型,所以我们不能传入任何东西。唯一的例外是null,它是每种类型的成员。



编辑:不要担心,当你开始使用它们时,这是对java通配符的正常误解。这就是为什么存在有界通配符(例如< ;? extends Something> )的原因,否则通用通配符几乎是无用的,因为编译器无法对其做任何假设。 b $ b

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.

解决方案

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:

Collection<?> c = new ArrayList<String>();
c.add(new Object()); // Compile time error

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.

这篇关于泛型错误:不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆