【The Java™ Tutorials】【Generics】4. Bounded Type Parameters

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 {

    private T t;          

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public  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 integerBox = new Box();
        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 {

    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

如果有多个限定,写法如下:


假设有3个限定A, B, C,其中A是一个类,B和C都是接口:

Class A { /* ... */ }
interface B { /* ... */ }
interface C { /* ... */ }

那么应该把A放在最前面,否则会报错:

class D  { /* ... */ }
class D  { /* ... */ }  // compile-time error

Generic Methods and Bounded Type Parameters

假设我们要实现以下代码的功能:

public static  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 {
    public int compareTo(T o);
}
public static > int countGreaterThan(T[] anArray, T elem) {
    int count = 0;
    for (T e : anArray)
        if (e.compareTo(elem) > 0)
            ++count;
    return count;
}

我们限定T必须实现Comparable接口,而Comparable接口中定义了比较两个T的大小的方法。

你可能感兴趣的:(【The Java™ Tutorials】【Generics】4. Bounded Type Parameters)