为什么要使用通配符捕获帮助器方法? [英] Why use a wild card capture helper method?

查看:269
本文介绍了为什么要使用通配符捕获帮助器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅:通配符捕获助手方法

p>

它说创建一个辅助方法来捕获通配符。

  public void foo(List<?> i){
fooHelper(i);
}
私人< T> void fooHelper(List< T> 1){
l.set(0,l.get(0));





$ b仅仅使用下面的这个函数不会产生任何编译错误,以相同的方式工作。我不明白的是:为什么你不使用这个,并避免使用助手?

  public< T> void foo(List< T> 1){
l.set(0,l.get(0));
}

我认为这个问题真的可以归结为:通配符有什么区别和泛型?所以,我去了这个:区别通配符和泛型
它说使用类型参数:

blockquote
1)如果你想在不同类型的方法参数上强加一些关系,你不能用通配符来做,你必须使用类型参数。

但是,这与辅助函数的通配符实际上并不完全相同吗?是不是在不同类型的方法参数上强制执行一个关系,其设置和获取未知值?



我的问题是:如果您必须定义需要关系的东西在不同类型的方法参数上,那么为什么首先使用通配符,然后使用辅助函数呢?



这似乎是一种混合通配符的方法。 / p>

解决方案

在这种特殊情况下,这是因为 List.set(int,E)方法要求类型与列表中的类型相同。

如果你没有helper方法,编译器不知道是否相同 List<?> 以及从 get(int)返回,所以你得到一个编译器错误:

 类型List< capture#1-of?>中的方法set(int,capture#1 of?不适用于参数(int,capture#2-of?)

使用helper方法,你告诉编译器,类型是一样的,我只是不知道类型是什么。



那么为什么有非辅助方法呢?
$ b 泛型直到Java 5才被引入,所以有很多代码在泛型之前。 Java之前的版本5 List 现在是 List<?> ,所以如果你试图编译旧代码一个通用的感知编译器,如果你不能改变方法签名,你将不得不添加这些帮助器方法。


Referring to : Wildcard Capture Helper Methods

It says to create a helper method to capture the wild card.

public void foo(List<?> i) {
    fooHelper(i);
}        
private <T> void fooHelper(List<T> l) {
    l.set(0, l.get(0));
}

Just using this function below alone doesn't produce any compilation errors, and seems to work the same way. What I don't understand is: why wouldn't you just use this and avoid using a helper?

public <T> void foo(List<T> l) {
    l.set(0, l.get(0));
}

I thought that this question would really boil down to: what's the difference between wildcard and generics? So, I went to this: difference between wildcard and generics. It says to use type parameters:

1) If you want to enforce some relationship on the different types of method arguments, you can't do that with wildcards, you have to use type parameters.

But, isn't that exactly what the wildcard with helper function is actually doing? Is it not enforcing a relationship on different types of method arguments with its setting and getting of unknown values?

My question is: If you have to define something that requires a relationship on different types of method args, then why use wildcards in the first place and then use a helper function for it?

It seems like a hacky way to incorporate wildcards.

解决方案

In this particular case it's because the List.set(int, E) method requires the type to be the same as the type in the list.

If you don't have the helper method, the compiler doesn't know if ? is the same for List<?> and the return from get(int) so you get a compiler error:

The method set(int, capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (int, capture#2-of ?)

With the helper method, you are telling the compiler, the type is the same, I just don't know what the type is.

So why have the non-helper method?

Generics weren't introduced until Java 5 so there is a lot of code out there that predates generics. A pre-Java 5 List is now a List<?> so if you were trying to compile old code in a generic aware compiler, you would have to add these helper methods if you couldn't change the method signatures.

这篇关于为什么要使用通配符捕获帮助器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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