The Java™ Tutorials — Generics :Generic Methods and Bounded Type Parameters 泛型方法和受限类型参数
来源:互联网 发布:mysql存放byteImage 编辑:程序博客网 时间:2024/06/05 22:38
The Java™ Tutorials — Generics :Generic Methods and Bounded Type Parameters 泛型方法和受限类型参数
原文地址:https://docs.oracle.com/javase/tutorial/java/generics/boundedTypeParams.html
关键点
- 泛型算法实现的关键:利用受限类型参数
全文翻译
Bounded type parameters are key to the implementation of generic algorithms. Consider the following method that counts the number of elements in an array T[] that are greater than a specified element elem.
受限泛型参数是实现泛型算法的关键。看看下面的方法,它会计算泛型数组中比指定元素elem更大的元素的个数。
public static <T> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e > elem) // compiler error
++count;
return count;
}
The implementation of the method is straightforward, but it does not compile because the greater than operator (>) applies only to primitive types such as short, int, double, long, float, byte, and char. You cannot use the > operator to compare objects. To fix the problem, use a type parameter bounded by the
Comparable<T>
interface:
方法的实现非常直接,但编不过。因为大于操作符(>)仅对原始类型如short、int,double、long、float、byte和char有效。你不能用“>”去比较对象。为了搞定这个问题,你需要使用一个受Comparable<T>
接口限定的类型参数。
public interface Comparable<T> {
public int compareTo(T o);
}
The resulting code will be:
代码就改写为了这样:
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;
}
- The Java™ Tutorials — Generics :Generic Methods and Bounded Type Parameters 泛型方法和受限类型参数
- The Java™ Tutorials — Generics :Bounded Type Parameters 受限的类型参数
- The Java™ Tutorials — Generics :Erasure of Generic Methods 泛型方法的类型擦除
- The Java™ Tutorials — Generics :Generic Methods 泛型方法
- The Java™ Tutorials — Generics :Effects of Type Erasure and Bridge Methods 类型擦除的影响以及桥方法
- The Java™ Tutorials — Generics :Wildcard Capture and Helper Methods 通配符匹配和辅助方法
- The Java™ Tutorials — Generics :Generics, Inheritance, and Subtypes 泛型,继承和子类型
- The Java™ Tutorials — Generics :Type Inference 类型推断
- The Java™ Tutorials — Generics :Type Erasure 类型消除
- The Java™ Tutorials — Generics :Generic Types 泛型
- The Java™ Tutorials — Generics :Wildcards and Subtyping 泛型和子类
- The Java™ Tutorials — Generics :Upper Bounded Wildcards 受上限控制的通配符
- The Java™ Tutorials — Generics :Lower Bounded Wildcards 有下限通配符
- The Java™ Tutorials — Generics :Raw Types 原始类型
- The Java™ Tutorials — Generics :Unbounded Wildcards 非受限通配符
- The Java™ Tutorials — Generics :Why Use Generics? 为什么使用泛型
- The Java™ Tutorials — Generics :Restrictions on Generics 泛型约束
- The Java™ Tutorials — Concurrency :Synchronized Methods 同步方法
- 操作系统中作业、进程、线程、管程各自的定义及联系
- 学习闭包的一些发现2
- IE6设置li的float:left,不能自适应宽的解决方法
- xen事件通道机制及其实例
- WAPG2 iOS App名称本地化(info.plist本地化)
- The Java™ Tutorials — Generics :Generic Methods and Bounded Type Parameters 泛型方法和受限类型参数
- HDU1232 畅通工程【并查集】
- MyBatis使用@MapKey注解接收多个查询记录到Map中,以便方便地用get()方法获取字段的值
- tomact
- HBase-5.提高HBase客户端的读写性能方法
- 音乐播放器单例
- LIS最长上升子序列模板
- Matlab里面的指针
- 轮播图封装