The Java™ Tutorials — Generics :Upper Bounded Wildcards 受上限控制的通配符

The Java™ Tutorials — Generics :Upper Bounded Wildcards 受上限控制的通配符

原文地址:https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html

关键点

  • 语法格式:<? extends XXX>
  • 优点:扩大兼容的范围

全文翻译

You can use an upper bounded wildcard to relax the restrictions on a variable. For example, say you want to write a method that works onList<Integer>List<Double>, and List<Number>; you can achieve this by using an upper bounded wildcard.

你可以使用一个受上限控制的通配符来放宽对一个变量的限制。例如,如果说你想写一个在List<Integer>List<Double>List<Number>上操作的方法,那么你就可以通过使用一个有上限通配符来达到这个目标。

To declare an upper-bounded wildcard, use the wildcard character (‘?’), followed by the extends keyword, followed by its upper bound. Note that, in this context, extends is used in a general sense to mean either “extends” (as in classes) or “implements” (as in interfaces).

为了声明一个有上限通配符,你需要在extends关键字前使用通配符“?”,并在extends关键字后加上上限类型。需要注意的是,在这个上下文中,extends是一种广义上的“扩展”,既包括了类扩展的意义,也包括了接口实现的意义。

To write the method that works on lists of Number and the subtypes of Number, such as Integer, Double, and Float, you would specify List<? extends Number>. The term List<Number> is more restrictive thanList<? extends Number> because the former matches a list of type Number only, whereas the latter matches a list of type Number or any of its subclasses.

为了写一个在Number或其子类类型(如Integer,Double和Float)列表上工作的方法,你需要指定List<? extends Number>类型。List<Number>要比List<? extends Number>更加严格,因为前者仅能匹配Number列表,然而后者却可同时匹配Number及其子类的列表。

Consider the following process method:

看下下面的process()方法:

public static void process(List<? extends Foo> list) { /* ... */ }

The upper bounded wildcard, <? extends Foo>, where Foo is any type, matches Foo and any subtype of Foo. The process method can access the list elements as type Foo:

有上限通配符<? extends Foo>中,Foo代表的是某个任意类型。此通配符可以匹配Foo和任何Foo的子类。这个process方法可以接受Foo类型的列表元素:

public static void process(List<? extends Foo> list) {
for (Foo elem : list) {
// ...
}
}

In the foreach clause, the elem variable iterates over each element in the list. Any method defined in the Foo class can now be used on elem.

在foreach子句中,变量elem遍历了列表中的每个元素。Foo类中定义的任何方法都可用在elem变量上。

The sumOfList method returns the sum of the numbers in a list:

方法sumOfList返回了列表元素之和:

public static double sumOfList(List<? extends Number> list) {
double s = 0.0;
for (Number n : list)
s += n.doubleValue();
return s;
}

The following code, using a list of Integer objects, prints sum = 6.0:

在下面的代码中,使用了一个Integer类型的列表,并打印出了sum = 6.0

List<Integer> li = Arrays.asList(1, 2, 3);
System.out.println("sum = " + sumOfList(li));

A list of Double values can use the same sumOfList method. The following code prints sum = 7.0:

一个Double的列表也可以使用sumOfList的方法。下面代码最终打印出的结果为sum = 7.0

List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
System.out.println("sum = " + sumOfList(ld));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值