有时您可能想限制可以在参数化类型中用作类型实参的类型。例如,对数字进行操作的方法可能只希望接受Number或其子类的实例。这就是有界类型参数(bounded type parameters)的用途。
要声明有界的类型形参,请列出类型形参的名称,后跟extends关键字,然后是其上界(**upper bound**(在此示例中为)Number。请注意,在这种情况下,在一般意义上,extends用于表示“扩展”(如在类中)或“实现”(如在接口中)。

  1. public class Box<T> {
  2. private T t;
  3. public void set(T t) {
  4. this.t = t;
  5. }
  6. public T get() {
  7. return t;
  8. }
  9. public <U extends Number> void inspect(U u){
  10. //有界类型形参U,其上界为Number
  11. System.out.println("T: " + t.getClass().getName());
  12. System.out.println("U: " + u.getClass().getName());
  13. }
  14. public static void main(String[] args) {
  15. Box<Integer> integerBox = new Box<Integer>();
  16. integerBox.set(new Integer(10));
  17. integerBox.inspect("some text"); // error: this is still String!
  18. }
  19. }

通过修改泛型方法以包含此有界类型形参,编译现在将失败,因为我们的调用inspect仍包括String

  1. Box.java:21: <U>inspect(U) in Box<java.lang.Integer> cannot
  2. be applied to (java.lang.String)
  3. integerBox.inspect("10");
  4. ^
  5. 1 error

除了可限制用于实例化泛型类型的类型之外,有界类型形参还允许您调用在边界中定义的方法:

  1. public class NaturalNumber<T extends Integer> {
  2. private T n;
  3. public NaturalNumber(T n) { this.n = n; }
  4. public boolean isEven() {
  5. return n.intValue() % 2 == 0;
  6. }
  7. // ...
  8. }

isEven 方法通过n调用Integer类中所定义的intValue方法。

多个界限

前面的示例说明了使用带单个界限的类型参数,但是一个类型参数可以具有多个界限(multiple bounds

  1. <T extends B1 & B2 & B3>

具有多个界限的类型变量是界限中列出的所有类型的子类型。如果范围之一是类,则必须首先指定它。例如:

  1. Class A { /* ... */ }
  2. interface B { /* ... */ }
  3. interface C { /* ... */ }
  4. class D <T extends A & B & C> { /* ... */ }

如果未首先指定A,则会出现编译时错误:

  1. class D <T extends B & A & C> { /* ... */ } // compile-time error