Java教程
  • Introduction
  • Getting Started
    • The Java Technology Phenomenon
      • About the Java Technology
      • What Can Java Technology Do?
      • How Will Java Technology Change My Life?
    • The Hello World Application
    • A Closer Look at the Hello World Application
  • Learning the Java Language
    • Object-Oriented Programming Concepts
      • What Is an Object?
      • What Is a Class?
      • What Is Inheritance?
      • What Is an Interface?
      • What Is a package?
    • Language Basics
      • Java Language Keywords
    • Annotations
      • Annotations Basics
      • Declaring an Annotation Type
      • Predefined Annotation Types
      • Repeating Annotations
      • Type Annotations and Pluggable Type Systems
    • Generics
      • Why Use Generics?
      • Generic Types
        • Raw Types
      • Generic Methods
      • Bounded Type Parameters
        • Generic Methods and Bounded Type Parameters
      • Generics, Inheritance, and Subtypes
      • Type Inference
      • Wildcards
        • Upper Bounded Wildcards
        • Unbounded Wildcards
        • Lower Bounded Wildcards
        • Wildcards and Subtyping
        • Wildcard Capture and Helper Methods
        • Guidelines for Wildcard Use
      • Type Erasure
        • Erasure of Generic Types
        • Erasure of Generic Methods
        • Effects of Type Erasure and Bridge Methods
        • Non-Reifiable Types
      • Restrictions on Generics
Powered by GitBook
On this page

Was this helpful?

  1. Learning the Java Language
  2. Generics

Bounded Type Parameters

有时我们想要限制参数化类型中所使用的类型参数。比如,一个操作数字的函数可能只接受Number类及其子类的实例。这就是我们所说的Bounded Type Parameters(有界类型参数)。

在定义有界类型参数时,先列出类型参数名,后面紧跟extends关键字,最后是参数的upper bound(上界),例子中我们用的是Number。请注意,在这语境中,extends被用在一个普通场景表示对类的extends(扩展)或者对接口的implements(实现)。

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!
    }
}

修改Box类中的泛型函数使得它包含有界类型参数,编译就会失败,因为我们在调用inspect时任然使用了String类型:

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

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

public class NaturalNumber<T extends Integer> {

    private T n;

    public NaturalNumber(T n)  { this.n = n; }

    public boolean isEven() {
        return n.intValue() % 2 == 0;
    }

    // ...
}

isEven函数中通过n调用Integer类中定义的intValue函数。

Multiple Bounds

上面的例子说明了单个上界类型的类型参数的使用方式,然而一个类型参数可以有Multiple Bounds(多个上界类型):

<T extends B1 & B2 & B3>

一个有多个上界类型的类型变量即为所有上界类型的子类型。如果其中一个上界类型是一个累的话,他应该出现在第一位,比如:

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

class D <T extends A & B & C> { /* ... */ }

如果类A没有出现在第一位,在编译过程中将会报错:

class D <T extends B & A & C> { /* ... */ }  // compile-time error
PreviousGeneric MethodsNextGeneric Methods and Bounded Type Parameters

Last updated 5 years ago

Was this helpful?