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

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

问题描述

我正在从 OracleDocGenericMethod 中阅读有关泛型方法的信息.当它说明何时使用通配符以及何时使用泛型方法时,我对比较感到非常困惑.引用文档.

<块引用>

接口集合{public boolean containsAll(Collection<?>c);public boolean addAll(Collection c);}

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

接口集合{公共 <T>boolean containsAll(Collection<T> c);public <T extends E>boolean addAll(Collection<T> c);//嘿,类型变量也可以有边界!}

[…]这告诉我们类型参数被用于多态性;它唯一的作用是允许多种实参类型用于不同的调用站点.如果是这种情况,应该使用通配符.通配符旨在支持灵活的子类型,这就是我们在这里想要表达的.

难道我们不认为像 (Collection c); 这样的通配符也支持多态性?那么为什么泛型方法的使用在这方面被认为不好呢?

继续前进,它指出,

<块引用>

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

这是什么意思?

他们已经给出了例子

<块引用>

class 集合 {公共静态<T>void copy(List dest, List src) {...}

[…]

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

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

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

有人可以照亮这个区域吗.

解决方案

在某些地方,通配符和类型参数的作用相同.但也有一些地方,你必须使用类型参数.

  1. 如果你想在不同类型的方法参数上强制执行某种关系,你不能用通配符来做到这一点,你必须使用类型参数.

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

public static void copy(List dest, List src)

在这里,您可以确保 destsrc 具有相同的 List 参数化类型.因此,将元素从 src 复制到 dest 是安全的.

但是,如果您继续更改方法以使用通配符:

public static void copy(List dest, List src)

它不会按预期工作.在第二种情况下,您可以将 ListList 作为 destsrc 传递.因此,将元素从 src 移动到 dest 将不再是类型安全的.如果你不需要这种关系,那么你完全可以不使用类型参数.

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

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

    public void print(List list)//OK

    但是你不能使用类型参数:

     public void print(List list)//不会编译


参考:

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天全站免登陆