自限定

自限定将强制泛型当做自己的边界参数来使用。自限定所做的,就是要求在继承关系中,像下面这样使用这个类:

class A extends SelfBounded<A> {}

它的意义是可以保证类型参数必须与正在被定义的类相同自限定只能强制作用于继承关系。如果使用自限定,就应该了解这个类所用的类型参数将与使用这个参数的类具有相同的基本类型。

下面是一个自限定的例子【1】:

 class SelfBounded<T extends SelfBounded<T>> {
T element;
SelfBounded<T> set(T arg) {
element = arg;
return this;
}
T get() { return element; }
} class A extends SelfBounded<A> {}
class B extends SelfBounded<A> {} // It's OK. class C extends SelfBounded<C> {
C setAndGet(C arg) { set(arg); return get(); }
} class D {}
// class E extends SelfBounded<D> {} // [Compile error]: Type parameter D is not within its bound public class SelfBounding {
public static void main(String[] args) {
A a = new A();
a.set(new A());
a = a.set(new A()).get();
a = a.get();
C c = new C();
c = c.setAndGet(new C());
}
}

我们发现class E是不能编译的。如果移除自限定这个限制(class SelfBounded<T>),这样E就可以编译了。但是就不能限制E这样的非自限定类型继承SelfBounded类了。

参数协变

先看一个协变返回类型的例子【2】:

 class Base {}
class Derived extends Base {} interface OrdinaryGetter {
Base get();
} interface DerivedGetter extends OrdinaryGetter {
// DerivedGetter.get()覆盖了OrdinaryGetter.get()
@Override Derived get();
} public class CovariantReturnTypes {
void test(DerivedGetter d) {
Derived result1 = d.get(); // 调用的DerivedGetter.get()
Base result2 = d.get(); // 也调用的DerivedGetter.get()
}
}

而自限定泛型将产生确切的导出类型作为其返回值。请看例【3】:

 interface GenericGetter<T extends GenericGetter<T>> {
T get();
} interface Getter extends GenericGetter<Getter> {} public class GenericsAndReturnTypes {
void test(Getter g) {
Getter result1 = g.get();
GenericGetter result2 = g.get(); // Also the base type
}
}

例【2】可以证明,返回值并不是区分两个不同方法的途径。而下面的例【4】则说明参数类型可以区分两个不同的方法。所以例【2】是覆盖(override)而例【4】是重载(overload)。

 class OrdinarySetter {
void set(Base base) {
System.out.println("OrdinarySetter.set(Base)");
}
} class DerivedSetter extends OrdinarySetter {
// @Override // [Compile Error]: Can't override. It's overload not override!
void set(Derived derived) {
System.out.println("DerivedSetter.set(Derived)");
}
} // 在非泛型代码中,参数类型不能随子类型发生变化。
public class OrdinaryArguments {
public static void main(String[] args) {
Base base = new Base();
Derived derived = new Derived();
DerivedSetter ds = new DerivedSetter();
ds.set(derived); // 调用DerivedSetter的set
ds.set(base); // 调用OrdinarySetter的set
}
}

但是,在使用自限定类型时,在导出类中只有一个方法,并且这个方法接受导出类型而不是基类型作为参数。例子【5】:

 interface SelfBoundSetter<T extends SelfBoundSetter<T>> {
void set(T arg);
} interface Setter extends SelfBoundSetter<Setter> {} public class SelfBoundingAndCovariantArguments {
void testA(Setter s1, Setter s2, SelfBoundSetter sb1, SelfBoundSetter sb2) {
s1.set(s2);
// 编译器不能识别将基类型当做参数传递给set的尝试,因为没有任何方法具有这样的签名。事实上,这个参数已经被覆盖。
// s1.set(sb1); // [Compile Error]: set(Setter) in SelfBoundSetter<Setter> cannot be applied to (SelfBoundSetter)
sb1.set(s1);
sb1.set(sb2);
}
}

如果上例不使用自限定类型,普通继承机制就会介入,而你将能够重载,就像在非泛型的情况下一样(自限定类型可以避免这种情况发生):

 class GenericSetter<T> { // Not self-bounded
void set(T arg){
System.out.println("GenericSetter.set(Base)");
}
} class DerivedGS extends GenericSetter<Base> {
// @Override // [Compile Error]: Can't override. It's overload not override.
void set(Derived derived){
System.out.println("DerivedGS.set(Derived)");
}
} public class PlainGenericInheritance {
public static void main(String[] args) {
Base base = new Base();
Derived derived = new Derived();
DerivedGS dgs = new DerivedGS();
dgs.set(derived);
dgs.set(base); // Compiles: overloaded, not overridden!
}
}
05-11 19:25