Skip to content

Commit 8266eeb

Browse files
author
siyuanzhou
committed
翻译20泛型到自限定类型“
1 parent 4d47d0a commit 8266eeb

1 file changed

Lines changed: 329 additions & 0 deletions

File tree

docs/book/20-Generics.md

Lines changed: 329 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,335 @@ class SelfBounded<T extends SelfBounded<T>> { // ...
35693569
为了理解自限定类型的含义,我们从这个惯用法的一个简单版本入手,它没有自限定的边界。
35703570
不能直接继承一个泛型参数,但是,可以继承在其自己的定义中使用这个泛型参数的类。也就是说,可以声明:
35713571

3572+
```java
3573+
// generics/CuriouslyRecurringGeneric.java
3574+
3575+
class GenericType<T> {}
3576+
3577+
public class CuriouslyRecurringGeneric
3578+
extends GenericType<CuriouslyRecurringGeneric> {}
3579+
```
3580+
3581+
这可以按照 Jim CoplienC++ 中的*古怪的循环模版模式*的命名方式,称为古怪的循环泛型(CRG)。“古怪的循环”是指类相当古怪地出现在它自己的基类中这一事实。
3582+
为了理解其含义,努力大声说:“我在创建一个新类,它继承自一个泛型类型,这个泛型类型接受我的类的名字作为其参数。”当给出导出类的名字时,这个泛型基类能够实现什么呢?好吧,Java中的泛型关乎参数和返回类型,因此它能够产生使用导出类作为其参数和返回类型的基类。它还能将导出类型用作其域类型,甚至那些将被擦除为 **Object** 的类型。下面是表示了这种情况的一个泛型类:
3583+
3584+
```java
3585+
// generics/BasicHolder.java
3586+
3587+
public class BasicHolder<T> {
3588+
T element;
3589+
void set(T arg) { element = arg; }
3590+
T get() { return element; }
3591+
void f() {
3592+
System.out.println(
3593+
element.getClass().getSimpleName());
3594+
}
3595+
}
3596+
```
3597+
3598+
这是一个普通的泛型类型,它的一些方法将接受和产生具有其参数类型的对象,还有一个方法将在其存储的域上执行操作(尽管只是在这个域上执行 **Object** 操作)。
3599+
我们可以在一个古怪的循环泛型中使用 **BasicHolder**
3600+
3601+
```java
3602+
// generics/CRGWithBasicHolder.java
3603+
3604+
class Subtype extends BasicHolder<Subtype> {}
3605+
3606+
public class CRGWithBasicHolder {
3607+
public static void main(String[] args) {
3608+
Subtype
3609+
st1 = new Subtype(),
3610+
st2 = new Subtype();
3611+
st1.set(st2);
3612+
Subtype st3 = st1.get();
3613+
st1.f();
3614+
}
3615+
}
3616+
/* Output:
3617+
Subtype
3618+
*/
3619+
```
3620+
3621+
注意,这里有些东西很重要:新类 **Subtype** 接受的参数和返回的值具有 **Subtype** 类型而不仅仅是基类 **BasicHolder** 类型。这就是 CRG 的本质:基类用导出类替代其参数。这意味着泛型基类变成了一种其所有导出类的公共功能的模版,但是这些功能对于其所有参数和返回值,将使用导出类型。也就是说,在所产生的类中将使用确切类型而不是基类型。因此,在**Subtype** 中,传递给 `set()` 的参数和从 `get()` 返回的类型都是确切的 **Subtype** 。
3622+
3623+
### 自限定
3624+
3625+
BasicHolder可以使用任何类型作为其泛型参数,就像下面看到的那样:
3626+
3627+
```java
3628+
// generics/Unconstrained.java
3629+
// (c)2017 MindView LLC: see Copyright.txt
3630+
// We make no guarantees that this code is fit for any purpose.
3631+
// Visit http://OnJava8.com for more book information.
3632+
3633+
class Other {}
3634+
class BasicOther extends BasicHolder<Other> {}
3635+
3636+
public class Unconstrained {
3637+
public static void main(String[] args) {
3638+
BasicOther b = new BasicOther();
3639+
BasicOther b2 = new BasicOther();
3640+
b.set(new Other());
3641+
Other other = b.get();
3642+
b.f();
3643+
}
3644+
}
3645+
/* Output:
3646+
Other
3647+
*/
3648+
```
3649+
3650+
限定将采取额外的步骤,强制泛型当作其自己的边界参数来使用。观察所产生的类可以如何使用以及不可以如何使用:
3651+
3652+
```java
3653+
// generics/SelfBounding.java
3654+
3655+
class SelfBounded<T extends SelfBounded<T>> {
3656+
T element;
3657+
SelfBounded<T> set(T arg) {
3658+
element = arg;
3659+
return this;
3660+
}
3661+
T get() { return element; }
3662+
}
3663+
3664+
class A extends SelfBounded<A> {}
3665+
class B extends SelfBounded<A> {} // Also OK
3666+
3667+
class C extends SelfBounded<C> {
3668+
C setAndGet(C arg) { set(arg); return get(); }
3669+
}
3670+
3671+
class D {}
3672+
// Can't do this:
3673+
// class E extends SelfBounded<D> {}
3674+
// Compile error:
3675+
// Type parameter D is not within its bound
3676+
3677+
// Alas, you can do this, so you cannot force the idiom:
3678+
class F extends SelfBounded {}
3679+
3680+
public class SelfBounding {
3681+
public static void main(String[] args) {
3682+
A a = new A();
3683+
a.set(new A());
3684+
a = a.set(new A()).get();
3685+
a = a.get();
3686+
C c = new C();
3687+
c = c.setAndGet(new C());
3688+
}
3689+
}
3690+
```
3691+
3692+
自限定所做的,就是要求在继承关系中,像下面这样使用这个类:
3693+
3694+
```java
3695+
class A extends SelfBounded<A>{}
3696+
```
3697+
3698+
这会强制要求将正在定义的类当作参数传递给基类。
3699+
自限定的参数有何意义呢?它可以保证类型参数必须与正在被定义的类相同。正如你在B类的定义中所看到的,还可以从使用了另一个 **SelfBounded** 参数的**SelfBounded** 中导出,尽管在 **A** 类看到的用法看起来是主要的用法。对定义 **E** 的尝试说明不能使用不是 **SelfBounded** 的类型参数。
3700+
遗憾的是, **F** 可以编译,不会有任何警告,因此自限定惯用法不是可强制执行的。如果它确实很重要,可以要求一个外部工具来确保不会使用原生类型来替代参数化类型。
3701+
注意,可以移除自限定这个限制,这样所有的类仍旧是可以编译的,但是 **E** 也会因此而变得可编译:
3702+
3703+
```java
3704+
// generics/NotSelfBounded.java
3705+
3706+
public class NotSelfBounded<T> {
3707+
T element;
3708+
NotSelfBounded<T> set(T arg) {
3709+
element = arg;
3710+
return this;
3711+
}
3712+
T get() { return element; }
3713+
}
3714+
3715+
class A2 extends NotSelfBounded<A2> {}
3716+
class B2 extends NotSelfBounded<A2> {}
3717+
3718+
class C2 extends NotSelfBounded<C2> {
3719+
C2 setAndGet(C2 arg) { set(arg); return get(); }
3720+
}
3721+
3722+
class D2 {}
3723+
// Now this is OK:
3724+
class E2 extends NotSelfBounded<D2> {}
3725+
```
3726+
3727+
因此很明显,自限定限制只能强制作用于继承关系。如果使用自限定,就应该了解这个类所用的类型参数将与使用这个参数的类具有相同的基类型。这会强制要求使用这个类的每个人都要遵循这种形式。
3728+
还可以将自限定用于泛型方法:
3729+
3730+
```java
3731+
// generics/SelfBoundingMethods.java
3732+
// (c)2017 MindView LLC: see Copyright.txt
3733+
// We make no guarantees that this code is fit for any purpose.
3734+
// Visit http://OnJava8.com for more book information.
3735+
3736+
public class SelfBoundingMethods {
3737+
static <T extends SelfBounded<T>> T f(T arg) {
3738+
return arg.set(arg).get();
3739+
}
3740+
public static void main(String[] args) {
3741+
A a = f(new A());
3742+
}
3743+
}
3744+
```
3745+
3746+
这可以防止这个方法被应用于除上述形式的自限定参数之外的任何事物上。
3747+
3748+
### 参数协变
3749+
3750+
自限定类型的价值在于它们可以产生*协变参数类型*——方法参数类型会随子类而变化。
3751+
3752+
尽管自限定类型还可以产生于子类类型相同的返回类型,但是这并不十分重要,因为*协变返回类型*是在 Java SE5 中引入的:
3753+
3754+
```java
3755+
// generics/CovariantReturnTypes.java
3756+
3757+
class Base {}
3758+
class Derived extends Base {}
3759+
3760+
interface OrdinaryGetter {
3761+
Base get();
3762+
}
3763+
3764+
interface DerivedGetter extends OrdinaryGetter {
3765+
// Overridden method return type can vary:
3766+
@Override
3767+
Derived get();
3768+
}
3769+
3770+
public class CovariantReturnTypes {
3771+
void test(DerivedGetter d) {
3772+
Derived d2 = d.get();
3773+
}
3774+
}
3775+
```
3776+
3777+
**DerivedGetter** 中的 `get()` 方法覆盖了 **OrdinaryGetter** 中的 `get()` ,并返回了一个从 `OrdinaryGetter.get()` 的返回类型中导出的类型。尽管这是完全合乎逻辑的事情(导出类方法应该能够返回比它覆盖的基类方法更具体的类型)但是这在早先的 Java 版本中是不合法的。
3778+
自限定泛型事实上将产生确切的导出类型作为其返回值,就像在 `get()` 中所看到的一样:
3779+
3780+
```java
3781+
// generics/GenericsAndReturnTypes.java
3782+
3783+
interface GenericGetter<T extends GenericGetter<T>> {
3784+
T get();
3785+
}
3786+
3787+
interface Getter extends GenericGetter<Getter> {}
3788+
3789+
public class GenericsAndReturnTypes {
3790+
void test(Getter g) {
3791+
Getter result = g.get();
3792+
GenericGetter gg = g.get(); // Also the base type
3793+
}
3794+
}
3795+
```
3796+
3797+
注意,这段代码不能编译,除非是使用囊括了协变返回类型的 Java SE5
3798+
3799+
然而,在非泛型代码中,参数类型不能随子类型发生变化:
3800+
3801+
```java
3802+
// generics/OrdinaryArguments.java
3803+
3804+
class OrdinarySetter {
3805+
void set(Base base) {
3806+
System.out.println("OrdinarySetter.set(Base)");
3807+
}
3808+
}
3809+
3810+
class DerivedSetter extends OrdinarySetter {
3811+
void set(Derived derived) {
3812+
System.out.println("DerivedSetter.set(Derived)");
3813+
}
3814+
}
3815+
3816+
public class OrdinaryArguments {
3817+
public static void main(String[] args) {
3818+
Base base = new Base();
3819+
Derived derived = new Derived();
3820+
DerivedSetter ds = new DerivedSetter();
3821+
ds.set(derived);
3822+
// Compiles--overloaded, not overridden!:
3823+
ds.set(base);
3824+
}
3825+
}
3826+
/* Output:
3827+
DerivedSetter.set(Derived)
3828+
OrdinarySetter.set(Base)
3829+
*/
3830+
```
3831+
3832+
`set(derived)` 和 `set(base)` 都是合法的,因此 `DerivedSetter.set()` 没有覆盖 `OrdinarySetter.set()` ,而是重载了这个方法。从输出中可以看到,在 **DerivedSetter** 中有两个方法,因此基类版本仍旧是可用的,因此可以证明它被重载过。
3833+
但是,在使用自限定类型时,在导出类中只有一个方法,并且这个方法接受导出类型而不是基类型为参数:
3834+
3835+
```java
3836+
// generics/SelfBoundingAndCovariantArguments.java
3837+
3838+
interface
3839+
SelfBoundSetter<T extends SelfBoundSetter<T>> {
3840+
void set(T arg);
3841+
}
3842+
3843+
interface Setter extends SelfBoundSetter<Setter> {}
3844+
3845+
public class SelfBoundingAndCovariantArguments {
3846+
void
3847+
testA(Setter s1, Setter s2, SelfBoundSetter sbs) {
3848+
s1.set(s2);
3849+
//- s1.set(sbs);
3850+
// error: method set in interface SelfBoundSetter<T>
3851+
// cannot be applied to given types;
3852+
// s1.set(sbs);
3853+
// ^
3854+
// required: Setter
3855+
// found: SelfBoundSetter
3856+
// reason: argument mismatch;
3857+
// SelfBoundSetter cannot be converted to Setter
3858+
// where T is a type-variable:
3859+
// T extends SelfBoundSetter<T> declared in
3860+
// interface SelfBoundSetter
3861+
// 1 error
3862+
}
3863+
}
3864+
```
3865+
3866+
编译器不能识别将基类型当作参数传递给 `set()` 的尝试,因为没有任何方法具有这样的签名。实际上,这个参数已经被覆盖。
3867+
如果不使用自限定类型,普通的继承机制就会介入,而你将能够重载,就像在非泛型的情况下一样:
3868+
3869+
```
3870+
// generics/PlainGenericInheritance.java
3871+
3872+
class GenericSetter<T> { // Not self-bounded
3873+
void set(T arg) {
3874+
System.out.println("GenericSetter.set(Base)");
3875+
}
3876+
}
3877+
3878+
class DerivedGS extends GenericSetter<Base> {
3879+
void set(Derived derived) {
3880+
System.out.println("DerivedGS.set(Derived)");
3881+
}
3882+
}
3883+
3884+
public class PlainGenericInheritance {
3885+
public static void main(String[] args) {
3886+
Base base = new Base();
3887+
Derived derived = new Derived();
3888+
DerivedGS dgs = new DerivedGS();
3889+
dgs.set(derived);
3890+
dgs.set(base); // Overloaded, not overridden!
3891+
}
3892+
}
3893+
/* Output:
3894+
DerivedGS.set(Derived)
3895+
GenericSetter.set(Base)
3896+
*/
3897+
```
3898+
3899+
这段代码在模仿 **OrdinaryArgument.java** ,在那个示例中,**DerivedSetter** 继承自包含一个 `set(Base)` 的**OrdinarySetter** 。而这里,**DerivedGS** 继承自泛型创建的也包含有一个 `set(Base)`的 `GenericSetter<Base>`。就像 **OrdinaryArgument.java** 一样,你可以从输出中看到, **DerivedGS** 包含两个 `set()` 的重载版本。如果不使用自限定,将重载参数类型。如果使用了自限定,只能获得某个方法的一个版本,它将接受确切的参数类型。
3900+
35723901
## 动态类型安全
35733902

35743903
<!-- Exceptions -->

0 commit comments

Comments
 (0)