何时使用通用方法以及何时使用通配符? [英] When to use generic methods and when to use wild-card?

查看:190
本文介绍了何时使用通用方法以及何时使用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 OracleDocGenericMethod 中的泛型方法。当它说什么时候使用通配符以及何时使用泛型方法时,我对这种比较相当困惑。
从文档中引用。


 界面集合< E> {
public boolean containsAll(Collection<?> c);
public boolean addAll(Collection< ;? extends E> c);

$ / code>

我们可以在这里使用泛型方法:

 界面集合< E> {
public< T> boolean containsAll(Collection< T> c);
public< T扩展E> boolean addAll(Collection< T> c);
//嘿,类型变量也可以有边界!
}

[...]
这告诉我们,用于多态性;
其唯一影响是允许在不同调用站点使用各种实际参数类型为
。如果是这种情况,应该使用通配符
。通配符旨在支持灵活的子类型,
这就是我们在这里要表达的内容。


认为通配符像(Collection<?extends E> c); 也支持
多态的类型?那么为什么通用方法的用法在这方面被认为是不好的? 继续前进,它表明,


泛型方法允许使用类型参数来表示方法
和/或其返回类型的一个或多个参数类型之间的
依赖关系。如果没有这样的依赖关系,就不应该使用通用的
方法。


这是什么意思? / p>

他们提供了示例


  class Collections {
public static< T> void copy(List< T> dest,List< ;? extends T> src){
...
}

[b]

我们可以用另一种方式为这种方法写签名:
,根本不使用通配符: / p>

  class集合{
public static< T,S extends T> void copy(List< T> dest,List< S> src){
...
}


该文档不鼓励第二种声明并促进使用第一种语法?第一个和第二个声明有什么区别?两者似乎都在做同样的事情?



有人可以照亮这个区域。

解决方案

在某些地方,通配符和类型参数可以执行相同的操作。但也有一些地方,你必须使用类型参数。


  1. 如果您想在不同类型的方法参数上强制执行某些关系,则不能使用通配符来完成此操作,必须使用类型参数。

以您的方法为例,假设您要确保 src dest 列表传递给 copy()方法应该是相同的参数化类型,您可以使用类型参数所以:

  public static< T extends Number> void copy(List< T> dest,List< T> src)

在这里, dest src 对于 List 具有相同的参数化类型。因此,将元素从 src 复制到 dest 是安全的。



但是,如果你继续改变使用通配符的方法:

  public static void copy(List< ;? extends Number> dest,List< ;? extends Number> src)

它无法按预期工作。在第二种情况下,您可以将 List< Integer> List< Float> 作为 dest src 。因此,将元素从 src 移动到 dest 将不再安全。
如果你不需要这种类型的关系,那么你完全可以自由地不使用类型参数。



使用通配符和类型之间的一些其他区别参数是:


  • 如果你只有一个参数化类型参数,那么你可以使用通配符,尽管类型参数也可以。
  • >
  • 类型参数支持多个边界,通配符不支持。
  • 通配符支持上下限,类型参数只支持上限。所以,如果你想定义一个方法,它需要一个 List 类型的 Integer 或者它是超类,你可以做:

      public void print(List< ;? super Integer> list)// OK 

    但您无法使用类型参数:

      public< T super Integer> void print(List< T> list)//不会编译







参考文献: angelika langer的Java泛型常见问题解答
>

I am reading about generic methods from OracleDocGenericMethod. I am pretty confused about the comparison when it says when to use wild-card and when to use generic methods. Quoting from the document.

interface Collection<E> {
    public boolean containsAll(Collection<?> c);
    public boolean addAll(Collection<? extends E> c);
}

We could have used generic methods here instead:

interface Collection<E> {
    public <T> boolean containsAll(Collection<T> c);
    public <T extends E> boolean addAll(Collection<T> c);
    // Hey, type variables can have bounds too!
}

[…] This tells us that the type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites. If that is the case, one should use wildcards. Wildcards are designed to support flexible subtyping, which is what we're trying to express here.

Don't we think wild card like (Collection<? extends E> c); is also supporting kind of polymorphism? Then why generic method usage is considered not good in this?

Continuing ahead, it states,

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used.

What does this mean?

They have presented the example

class Collections {
    public static <T> void copy(List<T> dest, List<? extends T> src) {
    ...
}

[…]

We could have written the signature for this method another way, without using wildcards at all:

class Collections {
    public static <T, S extends T> void copy(List<T> dest, List<S> src) {
    ...
}

The document discourages the second declaration and promotes usage of first syntax? What's the difference between the first and second declaration? Both seems to be doing the same thing?

Can someone put light on this area.

解决方案

There are certain places, where wildcards, and type parameters do the same thing. But there are also certain places, where you have 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.

Taking your method as example, suppose you want to ensure that the src and dest list passed to copy() method should be of same parameterized type, you can do it with type parameters like so:

public static <T extends Number> void copy(List<T> dest, List<T> src)

Here, you are ensured that both dest and src have same parameterized type for List. So, it's safe to copy elements from src to dest.

But, if you go on to change the method to use wildcard:

public static void copy(List<? extends Number> dest, List<? extends Number> src)

it won't work as expected. In 2nd case, you can pass List<Integer> and List<Float> as dest and src. So, moving elements from src to dest wouldn't be type safe anymore. If you don't need such kind of relation, then you are free not to use type parameters at all.

Some other difference between using wildcards and type parameters are:

  • If you have only one parameterized type argument, then you can use wildcard, although type parameter will also work.
  • Type parameters support multiple bounds, wildcards don't.
  • Wildcards support both upper and lower bounds, type parameters just support upper bounds. So, if you want to define a method that takes a List of type Integer or it's super class, you can do:

    public void print(List<? super Integer> list)  // OK
    

    but you can't use type parameter:

     public <T super Integer> void print(List<T> list)  // Won't compile
    


References:

这篇关于何时使用通用方法以及何时使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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