What and Why
There may be times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.
How
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound.
注意: In this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)
public class Box<T> {
private T t;
public void set(T t) {
this.t = t;
}
public T get() {
return t;
}
public <U extends Number> void inspect(U u){
System.out.println("T: " + t.getClass().getName());
System.out.println("U: " + u.getClass().getName());
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<Integer>();
integerBox.set(new Integer(10));
integerBox.inspect("some text"); // error: this is still String!
}
}
In addition to limiting the types you can use to instantiate a generic type, bounded type parameters allow you to invoke methods defined in the bounds:
public class NaturalNumber<T extends Integer> {
private T n;
public NaturalNumber(T n) { this.n = n; }
public boolean isEven() {
return n.intValue() % 2 == 0;
}
// ...
}
上面的例子中,由于限定了T是Integer的子类,所以n可以直接调用Integer中的方法intValue。
Multiple Bounds
如果有多个限定,写法如下:
<T extends B1 & B2 & B3>
假设有3个限定A, B, C,其中A是一个类,B和C都是接口:
Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }
那么应该把A放在最前面,否则会报错:
class D <T extends A & B & C> { /* ... */ }
class D <T extends B & A & C> { /* ... */ } // compile-time error
Generic Methods and Bounded Type Parameters
假设我们要实现以下代码的功能:
public static <T> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e > elem) // compiler error
++count;
return count;
}
但是这段代码会出现编译错误,因为T的具体类型是未知的,不能直接通过e > elem
判断大小(只有数值类型才能直接这样判断)。这个时候我们可以使用Bounded Type Parameters来很好的解决这个问题:
public interface Comparable<T> {
public int compareTo(T o);
}
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
我们限定T必须实现Comparable<T>接口,而Comparable<T>接口中定义了比较两个T的大小的方法。