mirror of
https://github.com/LingCoder/OnJava8.git
synced 2026-08-23 14:33:28 +08:00
翻译20泛型到通配符
This commit is contained in:
@@ -2758,25 +2758,574 @@ Java 7 引入了 **java.util.Objects** 库,使创建 `equals()` 和 `hashCode(
|
||||
|
||||
### 逆变
|
||||
|
||||
还可以走另外一条路,即使用超类型通配符。这里,可以声明通配符是由某个特定类的任何基类来界定的,方法是指定 `<?super MyClass>` ,甚至或者使用类型参数: `<?super T>`(尽管你不能对泛型参数给出一个超类型边界;即不能声明 `<T super MyClass>` )。这使得你可以安全地传递一个类型对象到泛型类型中。因此,有了超类型通配符,就可以向 **Collection** 写入了:
|
||||
|
||||
```java
|
||||
// generics/SuperTypeWildcards.java
|
||||
import java.util.*;
|
||||
public class SuperTypeWildcards {
|
||||
static void writeTo(List<? super Apple> apples) {
|
||||
apples.add(new Apple());
|
||||
apples.add(new Jonathan());
|
||||
// apples.add(new Fruit()); // Error
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
参数 **Apple** 是 **Apple** 的某种基类型的 **List**,这样你就知道向其中添加 **Apple** 或 **Apple** 的子类型是安全的。但是,既然 **Apple** 是下界,那么你可以知道向这样的 **List** 中添加 **Fruit** 是不安全的,因为这将使这个 **List** 敞开口子,从而可以向其中添加非 **Apple** 类型的对象,而这是违反静态类型安全的。
|
||||
因此你可能会根据如何能够向一个泛型类型“写入”(传递给一个方法),以及如何能够从一个泛型类型中“读取”(从一个方法中返回),来着手思考子类型和超类型边界。
|
||||
超类型边界放松了在可以向方法传递的参数上所作的限制,此示例提供了逆变和通配符的概述::
|
||||
|
||||
```java
|
||||
// generics/GenericReading.java
|
||||
import java.util.*;
|
||||
|
||||
public class GenericReading {
|
||||
static List<Apple> apples =
|
||||
Arrays.asList(new Apple());
|
||||
static List<Fruit> fruit = Arrays.asList(new Fruit());
|
||||
static <T> T readExact(List<T> list) {
|
||||
return list.get(0);
|
||||
}
|
||||
// A static method adapts to each call:
|
||||
static void f1() {
|
||||
Apple a = readExact(apples);
|
||||
Fruit f = readExact(fruit);
|
||||
f = readExact(apples);
|
||||
}
|
||||
// A class type is established
|
||||
// when the class is instantiated:
|
||||
static class Reader<T> {
|
||||
T readExact(List<T> list) { return list.get(0); }
|
||||
}
|
||||
static void f2() {
|
||||
Reader<Fruit> fruitReader = new Reader<>();
|
||||
Fruit f = fruitReader.readExact(fruit);
|
||||
//- Fruit a = fruitReader.readExact(apples);
|
||||
// error: incompatible types: List<Apple>
|
||||
// cannot be converted to List<Fruit>
|
||||
}
|
||||
static class CovariantReader<T> {
|
||||
T readCovariant(List<? extends T> list) {
|
||||
return list.get(0);
|
||||
}
|
||||
}
|
||||
static void f3() {
|
||||
CovariantReader<Fruit> fruitReader =
|
||||
new CovariantReader<>();
|
||||
Fruit f = fruitReader.readCovariant(fruit);
|
||||
Fruit a = fruitReader.readCovariant(apples);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
f1(); f2(); f3();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
第一个方法 `readExact()` 使用了精确的类型。因此如果使用这个没有任何通配符的精确类型,就可以向 **List** 中写入和读取这个精确类型。另外,对于返回值,静态的泛型方法 `readExact()` 可以有效地“适应”每个方法调用,并能够从 `List<Apple>` 中返回一个 **Apple** ,从 `List<Fruit>` 中返回一个 **Fruit** ,就像在 `f1()` 中看到的那样。因此,如果可以摆脱静态泛型方法,那么当只是读取时,就不需要协变类型了。
|
||||
但是,如果有一个泛型类,那么当你创建这个类的实例时,要为这个类确定参数。就像在 `f2()` 中看到的,**fruitReader** 实例可以从 `List<Fruit>` 中读取一个 **Fruit** ,因为这就是它的确切类型。但是 `List<Apple>` 还应该产生 **Fruit** 对象,而 **fruitReader** 不允许这么做。
|
||||
为了修正这个问题,`CovariantReader.readCovcariant()` 方法将接受 `List<?extendsT>` ,因此,从这个列表中读取一个 **T** 是安全的(你知道在这个列表中的所有对象至少是一个 **T** ,并且可能是从T导出的某种对象)。在 `f3()` 中,你可以看到现在可以从 `List<Apple>` 中读取 **Fruit** 了。
|
||||
|
||||
### 无界通配符
|
||||
|
||||
无界通配符<?>看起来意味着“任何事物”,因此使用无界通配符好像等价于使用原生类型。事实上,编译器初看起来是支持这种判断的:
|
||||
|
||||
```java
|
||||
// generics/UnboundedWildcards1.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.util.*;
|
||||
|
||||
public class UnboundedWildcards1 {
|
||||
static List list1;
|
||||
static List<?> list2;
|
||||
static List<? extends Object> list3;
|
||||
static void assign1(List list) {
|
||||
list1 = list;
|
||||
list2 = list;
|
||||
//- list3 = list;
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// list3 = list;
|
||||
// ^
|
||||
// required: List<? extends Object>
|
||||
// found: List
|
||||
}
|
||||
static void assign2(List<?> list) {
|
||||
list1 = list;
|
||||
list2 = list;
|
||||
list3 = list;
|
||||
}
|
||||
static void assign3(List<? extends Object> list) {
|
||||
list1 = list;
|
||||
list2 = list;
|
||||
list3 = list;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
assign1(new ArrayList());
|
||||
assign2(new ArrayList());
|
||||
//- assign3(new ArrayList());
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method assign3 in class UnboundedWildcards1
|
||||
// is applied to given types
|
||||
// assign3(new ArrayList());
|
||||
// ^
|
||||
// required: List<? extends Object>
|
||||
// found: ArrayList
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// assign3(new ArrayList());
|
||||
// ^
|
||||
// required: List<? extends Object>
|
||||
// found: ArrayList
|
||||
// 2 warnings
|
||||
assign1(new ArrayList<>());
|
||||
assign2(new ArrayList<>());
|
||||
assign3(new ArrayList<>());
|
||||
// Both forms are acceptable as List<?>:
|
||||
List<?> wildList = new ArrayList();
|
||||
wildList = new ArrayList<>();
|
||||
assign1(wildList);
|
||||
assign2(wildList);
|
||||
assign3(wildList);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
有很多情况都和你在这里看到的情况类似,即编译器很少关心使用的是原生类型还是 `<?>` 。在这些情况中,`<?>` 可以被认为是一种装饰,但是它仍旧是很有价值的,因为,实际上,它是在声明:“我是想用Java的泛型来编写这段代码,我在这里并不是要用原生类型,但是在当前这种情况下,泛型参数可以持有任何类型。”
|
||||
第二个示例展示了无界通配符的一个重要应用。当你在处理多个泛型参数时,有时允许一个参数可以是任何类型,同时为其他参数确定某种特定类型的这种能力会显得很重要:
|
||||
|
||||
```java
|
||||
// generics/UnboundedWildcards2.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.util.*;
|
||||
|
||||
public class UnboundedWildcards2 {
|
||||
static Map map1;
|
||||
static Map<?,?> map2;
|
||||
static Map<String,?> map3;
|
||||
static void assign1(Map map) { map1 = map; }
|
||||
static void assign2(Map<?,?> map) { map2 = map; }
|
||||
static void assign3(Map<String,?> map) { map3 = map; }
|
||||
public static void main(String[] args) {
|
||||
assign1(new HashMap());
|
||||
assign2(new HashMap());
|
||||
//- assign3(new HashMap());
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method assign3 in class UnboundedWildcards2
|
||||
// is applied to given types
|
||||
// assign3(new HashMap());
|
||||
// ^
|
||||
// required: Map<String,?>
|
||||
// found: HashMap
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// assign3(new HashMap());
|
||||
// ^
|
||||
// required: Map<String,?>
|
||||
// found: HashMap
|
||||
// 2 warnings
|
||||
assign1(new HashMap<>());
|
||||
assign2(new HashMap<>());
|
||||
assign3(new HashMap<>());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
但是,当你拥有的全都是无界通配符时,就像在 `Map<?,?>` 中看到的那样,编译器看起来就无法将其与原生 **Map** 区分开了。另外, **UnboundedWildcards.java** 展示了编译器处理 `List<?>` 和 `List<? extends Object>` 时是不同的。
|
||||
令人困惑的是,编译器并非总是关注像 `List` 和 `List<?>` 之间的这种差异,因此它们看起来就像是相同的事物。因为,事实上,由于泛型参数将擦除到它的第一个边界,因此 `List<?>` 看起来等价于 `List<Object>` ,而 **List** 实际上也是 `List<Object>` ——除非这些语句都不为真。**List** 实际上表示“持有任何 **Object** 类型的原生 **List ** ”,而 `List<?>` 表示“具有某种特定类型的非原生 **List** ,只是我们不知道那种类型是什么。”
|
||||
编译器何时才会关注原生类型和涉及无界通配符的类型之间的差异呢?下面的示例使用了前面定义的 `Holder<T>` 类,它包含接受 **Holder** 作为参数的各种方法,但是它们具有不同的形式:
|
||||
作为原生类型,具有具体的类型参数以及具有无界通配符参数:
|
||||
|
||||
```java
|
||||
// generics/Wildcards.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// Exploring the meaning of wildcards
|
||||
|
||||
public class Wildcards {
|
||||
// Raw argument:
|
||||
static void rawArgs(Holder holder, Object arg) {
|
||||
//- holder.set(arg);
|
||||
// warning: [unchecked] unchecked call to set(T)
|
||||
// as a member of the raw type Holder
|
||||
// holder.set(arg);
|
||||
// ^
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in class Holder
|
||||
// 1 warning
|
||||
|
||||
// Can't do this; don't have any 'T':
|
||||
// T t = holder.get();
|
||||
|
||||
// OK, but type information is lost:
|
||||
Object obj = holder.get();
|
||||
}
|
||||
// Like rawArgs(), but errors instead of warnings:
|
||||
static void
|
||||
unboundedArg(Holder<?> holder, Object arg) {
|
||||
//- holder.set(arg);
|
||||
// error: method set in class Holder<T>
|
||||
// cannot be applied to given types;
|
||||
// holder.set(arg);
|
||||
// ^
|
||||
// required: CAP#1
|
||||
// found: Object
|
||||
// reason: argument mismatch;
|
||||
// Object cannot be converted to CAP#1
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in class Holder
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Object from capture of ?
|
||||
// 1 error
|
||||
|
||||
// Can't do this; don't have any 'T':
|
||||
// T t = holder.get();
|
||||
|
||||
// OK, but type information is lost:
|
||||
Object obj = holder.get();
|
||||
}
|
||||
static <T> T exact1(Holder<T> holder) {
|
||||
return holder.get();
|
||||
}
|
||||
static <T> T exact2(Holder<T> holder, T arg) {
|
||||
holder.set(arg);
|
||||
return holder.get();
|
||||
}
|
||||
static <T>
|
||||
T wildSubtype(Holder<? extends T> holder, T arg) {
|
||||
//- holder.set(arg);
|
||||
// error: method set in class Holder<T#2>
|
||||
// cannot be applied to given types;
|
||||
// holder.set(arg);
|
||||
// ^
|
||||
// required: CAP#1
|
||||
// found: T#1
|
||||
// reason: argument mismatch;
|
||||
// T#1 cannot be converted to CAP#1
|
||||
// where T#1,T#2 are type-variables:
|
||||
// T#1 extends Object declared in method
|
||||
// <T#1>wildSubtype(Holder<? extends T#1>,T#1)
|
||||
// T#2 extends Object declared in class Holder
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends T#1 from
|
||||
// capture of ? extends T#1
|
||||
// 1 error
|
||||
|
||||
return holder.get();
|
||||
}
|
||||
static <T>
|
||||
void wildSupertype(Holder<? super T> holder, T arg) {
|
||||
holder.set(arg);
|
||||
//- T t = holder.get();
|
||||
// error: incompatible types:
|
||||
// CAP#1 cannot be converted to T
|
||||
// T t = holder.get();
|
||||
// ^
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in method
|
||||
// <T>wildSupertype(Holder<? super T>,T)
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Object super:
|
||||
// T from capture of ? super T
|
||||
// 1 error
|
||||
|
||||
// OK, but type information is lost:
|
||||
Object obj = holder.get();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
Holder raw = new Holder<>();
|
||||
// Or:
|
||||
raw = new Holder();
|
||||
Holder<Long> qualified = new Holder<>();
|
||||
Holder<?> unbounded = new Holder<>();
|
||||
Holder<? extends Long> bounded = new Holder<>();
|
||||
Long lng = 1L;
|
||||
|
||||
rawArgs(raw, lng);
|
||||
rawArgs(qualified, lng);
|
||||
rawArgs(unbounded, lng);
|
||||
rawArgs(bounded, lng);
|
||||
|
||||
unboundedArg(raw, lng);
|
||||
unboundedArg(qualified, lng);
|
||||
unboundedArg(unbounded, lng);
|
||||
unboundedArg(bounded, lng);
|
||||
|
||||
//- Object r1 = exact1(raw);
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method exact1 in class Wildcards is applied
|
||||
// to given types
|
||||
// Object r1 = exact1(raw);
|
||||
// ^
|
||||
// required: Holder<T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact1(Holder<T>)
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// Object r1 = exact1(raw);
|
||||
// ^
|
||||
// required: Holder<T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact1(Holder<T>)
|
||||
// 2 warnings
|
||||
|
||||
Long r2 = exact1(qualified);
|
||||
Object r3 = exact1(unbounded); // Must return Object
|
||||
Long r4 = exact1(bounded);
|
||||
|
||||
//- Long r5 = exact2(raw, lng);
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method exact2 in class Wildcards is
|
||||
// applied to given types
|
||||
// Long r5 = exact2(raw, lng);
|
||||
// ^
|
||||
// required: Holder<T>,T
|
||||
// found: Holder,Long
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact2(Holder<T>,T)
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// Long r5 = exact2(raw, lng);
|
||||
// ^
|
||||
// required: Holder<T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact2(Holder<T>,T)
|
||||
// 2 warnings
|
||||
|
||||
Long r6 = exact2(qualified, lng);
|
||||
|
||||
//- Long r7 = exact2(unbounded, lng);
|
||||
// error: method exact2 in class Wildcards
|
||||
// cannot be applied to given types;
|
||||
// Long r7 = exact2(unbounded, lng);
|
||||
// ^
|
||||
// required: Holder<T>,T
|
||||
// found: Holder<CAP#1>,Long
|
||||
// reason: inference variable T has
|
||||
// incompatible bounds
|
||||
// equality constraints: CAP#1
|
||||
// lower bounds: Long
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact2(Holder<T>,T)
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Object from capture of ?
|
||||
// 1 error
|
||||
|
||||
//- Long r8 = exact2(bounded, lng);
|
||||
// error: method exact2 in class Wildcards
|
||||
// cannot be applied to given types;
|
||||
// Long r8 = exact2(bounded, lng);
|
||||
// ^
|
||||
// required: Holder<T>,T
|
||||
// found: Holder<CAP#1>,Long
|
||||
// reason: inference variable T
|
||||
// has incompatible bounds
|
||||
// equality constraints: CAP#1
|
||||
// lower bounds: Long
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>exact2(Holder<T>,T)
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Long from
|
||||
// capture of ? extends Long
|
||||
// 1 error
|
||||
|
||||
//- Long r9 = wildSubtype(raw, lng);
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method wildSubtype in class Wildcards
|
||||
// is applied to given types
|
||||
// Long r9 = wildSubtype(raw, lng);
|
||||
// ^
|
||||
// required: Holder<? extends T>,T
|
||||
// found: Holder,Long
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSubtype(Holder<? extends T>,T)
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// Long r9 = wildSubtype(raw, lng);
|
||||
// ^
|
||||
// required: Holder<? extends T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSubtype(Holder<? extends T>,T)
|
||||
// 2 warnings
|
||||
|
||||
Long r10 = wildSubtype(qualified, lng);
|
||||
// OK, but can only return Object:
|
||||
Object r11 = wildSubtype(unbounded, lng);
|
||||
Long r12 = wildSubtype(bounded, lng);
|
||||
|
||||
//- wildSupertype(raw, lng);
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method wildSupertype in class Wildcards
|
||||
// is applied to given types
|
||||
// wildSupertype(raw, lng);
|
||||
// ^
|
||||
// required: Holder<? super T>,T
|
||||
// found: Holder,Long
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSupertype(Holder<? super T>,T)
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// wildSupertype(raw, lng);
|
||||
// ^
|
||||
// required: Holder<? super T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSupertype(Holder<? super T>,T)
|
||||
// 2 warnings
|
||||
|
||||
wildSupertype(qualified, lng);
|
||||
|
||||
//- wildSupertype(unbounded, lng);
|
||||
// error: method wildSupertype in class Wildcards
|
||||
// cannot be applied to given types;
|
||||
// wildSupertype(unbounded, lng);
|
||||
// ^
|
||||
// required: Holder<? super T>,T
|
||||
// found: Holder<CAP#1>,Long
|
||||
// reason: cannot infer type-variable(s) T
|
||||
// (argument mismatch; Holder<CAP#1>
|
||||
// cannot be converted to Holder<? super T>)
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSupertype(Holder<? super T>,T)
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Object from capture of ?
|
||||
// 1 error
|
||||
|
||||
//- wildSupertype(bounded, lng);
|
||||
// error: method wildSupertype in class Wildcards
|
||||
// cannot be applied to given types;
|
||||
// wildSupertype(bounded, lng);
|
||||
// ^
|
||||
// required: Holder<? super T>,T
|
||||
// found: Holder<CAP#1>,Long
|
||||
// reason: cannot infer type-variable(s) T
|
||||
// (argument mismatch; Holder<CAP#1>
|
||||
// cannot be converted to Holder<? super T>)
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>wildSupertype(Holder<? super T>,T)
|
||||
// where CAP#1 is a fresh type-variable:
|
||||
// CAP#1 extends Long from capture of
|
||||
// ? extends Long
|
||||
// 1 error
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- Issues -->
|
||||
|
||||
在 `rawArgs()` 中,编译器知道 `Holder` 是一个泛型类型,因此即使它在这里被表示成一个原生类型,编译器仍旧知道向 `set()` 传递一个 **Object** 是不安全的。由于它是原生类型,你可以将任何类型的对象传递给 `set()` ,而这个对象将被向上转型为 **Object** 。因此,无论何时;只要使用了原生类型,都会放弃编译期检查。对 `get()` 的调用说明了相同的问题:没有任何 **T** 类型的对象,因此结果只能是一个 **Object** 。
|
||||
人们很自然地会开始考虑原生 `Holder` 与 `Holder<?>` 是大致相同的事物。但是 `unboundedArg()` 强调它们是不同的——它揭示了相同的问题,但是它将这些问题作为错误而不是警告报告,因为原生 **Holder** 将持有任何类型的组合,而 `Holder<?>` 将持有具有某种具体类型的同构集合,因此不能只是向其中传递 **Object** 。
|
||||
在 `exact1()` 和 `exact2()` 中,你可以看到使用了确切的泛型参数——没有任何通配符。你将看到,`exact2()`与 `exact1()` 具有不同的限制,因为它有额外的参数。
|
||||
在 `wildSubtype()` 中,在 **Holder** 类型上的限制被放松为包括持有任何扩展自 **T** 的对象的 **Holder** 。这还是意味着如果T是 **Fruit** ,那么 `holder` 可以是 `Holder<Apple>` ,这是合法的。为了防止将 **Orange** 放置到 `Holder<Apple>` 中,对 `set()` 的调用(或者对任何接受这个类型参数为参数的方法的调用)都是不允许的。但是,你仍旧知道任何来自 `Holder<?extends Fruit>` 的对象至少是 **Fruit** ,因此 `get()` (或者任何将产生具有这个类型参数的返回值的方法)都是允许的。
|
||||
`wildSupertype()` 展示了超类型通配符,这个方法展示了与 `wildSubtype()` 相反的行为:`holder` 可以是持有任何T的基类型的容器。因此, `set()` 可以接受**T** ,因为任何可以工作于基类的对象都可以多态地作用于导出类(这里就是 **T** )。但是,尝试着调用 `get()` 是没有用的,因为由 `holder` 持有的类型可以是任何超类型,因此唯一安全的类型就是 **Object** 。
|
||||
这个示例还展示了对于在 `unbounded()` 中使用无界通配符能够做什么不能做什么所做出的限制。对于迁移兼容性,`rawArgs()` 将接受所有 **Holder** 的不同变体,而不会产生警告。`unboundedArg()` 方法也可以接受相同的所有类型,尽管如前所述,它在方法体内部处理这些类型的方式并不相同。
|
||||
|
||||
如果向接受“确切”泛型类型(没有通配符)的方法传递一个原生 **Holder** 引用,就会得到一个警告,因为确切的参数期望得到在原生类型中并不存在的信息。如果向 `exact1()` 传递一个无界引用,就不会有任何可以确定返回类型的类型信息。
|
||||
可以看到,`exact2()` 具有最多的限制,因为它希望精确地得到一个 `Holder<T>` ,以及一个具有类型 **T** 的参数,正由于此,它将产生错误或警告,除非提供确切的参数。有时,这样做很好,但是如果它过于受限,那么就可以使用通配符,这取决于是否想要从泛型参数中返回类型确定的返回值(就像在 `wildSubtype()` 中看到的那样),或者是否想要向泛型参数传递类型确定的参数(就像在 `wildSupertype()` 中看到的那样)。
|
||||
因此,使用确切类型来替代通配符类型的好处是,可以用泛型参数来做更多的事,但是使用通配符使得你必须接受范围更宽的参数化类型作为参数。因此,必须逐个情况地权衡利弊,找到更适合你的需求的方法。
|
||||
|
||||
### 捕获转换
|
||||
|
||||
有一种情况特别需要使用 `<?>`而不是原生类型。如果向一个使用 `<?>` 的方法传递原生类型,那么对编译器来说,可能会推断出实际的类型参数,使得这个方法可以回转并调用另一个使用这个确切类型的方法。下面的示例演示了这种技术,它被称为捕获转换,因为未指定的通配符类型被捕获,并被转换为确切类型。这里,有关警告的注释只有在 `@SuppressWarnings` 注解被移除之后才能起作用:
|
||||
|
||||
```java
|
||||
// generics/CaptureConversion.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
|
||||
public class CaptureConversion {
|
||||
static <T> void f1(Holder<T> holder) {
|
||||
T t = holder.get();
|
||||
System.out.println(t.getClass().getSimpleName());
|
||||
}
|
||||
static void f2(Holder<?> holder) {
|
||||
f1(holder); // Call with captured type
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void main(String[] args) {
|
||||
Holder raw = new Holder<>(1);
|
||||
|
||||
f1(raw);
|
||||
// warning: [unchecked] unchecked method invocation:
|
||||
// method f1 in class CaptureConversion
|
||||
// is applied to given types
|
||||
// f1(raw);
|
||||
// ^
|
||||
// required: Holder<T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>f1(Holder<T>)
|
||||
// warning: [unchecked] unchecked conversion
|
||||
// f1(raw);
|
||||
// ^
|
||||
// required: Holder<T>
|
||||
// found: Holder
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in
|
||||
// method <T>f1(Holder<T>)
|
||||
// 2 warnings
|
||||
|
||||
f2(raw); // No warnings
|
||||
Holder rawBasic = new Holder();
|
||||
|
||||
rawBasic.set(new Object());
|
||||
// warning: [unchecked] unchecked call to set(T)
|
||||
// as a member of the raw type Holder
|
||||
// rawBasic.set(new Object());
|
||||
// ^
|
||||
// where T is a type-variable:
|
||||
// T extends Object declared in class Holder
|
||||
// 1 warning
|
||||
|
||||
f2(rawBasic); // No warnings
|
||||
// Upcast to Holder<?>, still figures it out:
|
||||
Holder<?> wildcarded = new Holder<>(1.0);
|
||||
f2(wildcarded);
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
Integer
|
||||
Integer
|
||||
Object
|
||||
Double
|
||||
*/
|
||||
```
|
||||
|
||||
`f1()` 中的类型参数都是确切的,没有通配符或边界。在 `f2()` 中,**Holder** 参数是一个无界通配符,因此它看起来是未知的。但是,在 `f2()` 中,`f1()` 被调用,而 `f1()` 需要一个已知参数。这里所发生的是:参数类型在调用 `f2()` 的过程中被捕获,因此它可以在对 `f1()` 的调用中被使用。
|
||||
你可能想知道,这项技术是否可以用于写入,但是这要求要在传递 `Holder<?>`时同时传递一个具体类型。捕获转换只有在这样的情况下可以工作:即在方法内部,你需要使用确切的类型。注意,不能从 `f2()`中返回 **T**,因为 **T ** 对于 `f2()` 来说是未知的。捕获转换十分有趣,但是非常受限。
|
||||
|
||||
## 问题
|
||||
|
||||
<!-- Self-Bounded Types -->
|
||||
|
||||
## 自我约束类型
|
||||
## 自限定的类型
|
||||
|
||||
<!-- Dynamic Type Safety -->
|
||||
|
||||
Java泛型中会定期出现一种让人费解的习惯用法。 看起来是这样的:
|
||||
在Java泛型中,有一个好像是经常性出现的惯用法,它相当令人费解:
|
||||
|
||||
```
|
||||
class SelfBounded<T extends SelfBounded<T>> { // ...
|
||||
```
|
||||
|
||||
这具有两个指向彼此的镜子的眩晕效果,这是一种无限的反射。 **SelfBounded**类采用通用参数`T`,`T`受一个界限约束,该界限为**SelfBounded**,其中`T`为参数。
|
||||
这就像两面镜子彼此照向对方所引起的目眩效果一样,是一种无限反射。**SelfBounded** 类接受泛型参数 **T**,而T由一个边界类限定,这个边界就是拥有 **T** 作为其参数的 **SelfBounded**。
|
||||
当你首次看到它时,很难去解析它,它强调的是当 `extends` 关键字用于边界与用来创建子类明显是不同的。
|
||||
|
||||
## 动态类型安全
|
||||
|
||||
|
||||
Reference in New Issue
Block a user