The Java™ Tutorials — Generics :Bounded Type Parameters 受限的类型参数

The Java™ Tutorials — Generics :Bounded Type Parameters 受限的类型参数

原文地址:https://docs.oracle.com/javase/tutorial/java/generics/bounded.html

关键点

  • 功能:对泛型变量的范围作出限制
  • 格式: 
    • 单一限制:<U extends Number>
    • 多种限制:<U extends A & B & C>
  • extends表达的意义:这里指的是广义上“扩展”,兼有“类继承”和“接口实现”之意
  • 多种限制下的格式语法要求:如果上限类型是一个类,必须第一位标出,否则编译错误 
    • 题外问题:如果多种限制中的A和B同时为类该何如? 
      • 答:编译错误,这违反了Java不许多重继承的原则

全文翻译

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.

有些时候,你希望对在参数化类型中可被作为类型变量的类型做出限制。例如,一个在数字上操作的方法也许仅仅希望接受Number及其子类的实例。而这就是受限类型参数的功能所在。

To declare a bounded type parameter, list the type parameter’s name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either “extends” (as in classes) or “implements” (as in interfaces).

为了声明一个受限类型参数,你需要在extends关键字前列出类型参数名,并在其后注明它的上限类型。对于本例而言,这个上限类型就是Number。需要注意的是,在这种环境中,extends被用为一种广义上的“扩展”。也就是说,它即表示类中的“扩展”,也表示接口中的“实现”。

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

By modifying our generic method to include this bounded type parameter, compilation will now fail, since our invocation of inspect still includes a String:

在修改了我们的泛型方法,以让其包含受限类型参数后,编译就会失败。因为我们对于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

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

// ...
}

The isEven method invokes the intValue method defined in the Integer class through n.

这个isEven()方法就通过变量n调用了Integer类中定义的intValue()方法。

Multiple Bounds 多种限制

The preceding example illustrates the use of a type parameter with a single bound, but a type parameter can have multiple bounds:

之前的例子说明了如何对一个类型参数做出一个单一的限定。不过一个类型参数是可有多种限定的:

<T extends B1 & B2 & B3>

A type variable with multiple bounds is a subtype of all the types listed in the bound. If one of the bounds is a class, it must be specified first. For example:

在受限类型范围中罗列着所有的上限类型,而一个带有多类型限制的类型变量就是这些类型的子类型。如果其中一个上限类型为一个class,那么它就必须在第一位列出。例如:

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

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

If bound A is not specified first, you get a compile-time error:

如果上限范围A没有在第一位列出,你就会收到一个编译时错误:

class D <T extends B & A & C> { /* ... */ }  // compile-time error
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值