如何理解<T extends Comparable<? super T>>
在看java容器类的时候经常可以看到<T extends Comparable<? super T>>,感觉十分不解?
我们觉得<T extends Comparable<T>>我们是知道的,T是实现了Comparable接口的类型,因此他们之间可以进行比较,
<? super T>表名类型参数需要是T或T的父类,那么<T extends Comparable<? super T>>到底是什么意思呢?
对T的约束是Comparable的子类,对Comparable中泛型的约束是,?至少是T的父类,那么就意味着T是?的子类。
一言以蔽之:元素必须是实现了Comparable接口的类或者其子类,可以使用父类方法比较子类型
/**
* Created by wangbin10 on 2018/9/18.
* mySort2() list中的元素必须是实现了Comparable接口的类或者其子类
* Java采取的是类型擦除的方法来实现泛型,并通过extends和super关键字来约束泛型的上界和下界。
* extends关键字用于确定泛型的上界。<A extends B>表示类B或者其子类。
* super关键字用于确定泛型的下界,<A super B>表示类B或者其父类,一直到Object。?则是一个通配符。
* 因此,<T extends Comparable<? super T>>表示了上界为实现了Comparable接口的类,<? super T>则表示实现Comparable接口的类的子类也可以,从而确定下界
*/
public class Test {
public static void main(String[] args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Animal(25));
animals.add(new Dog(34));
mySort1(animals);//ok List<Dog> dogs = new ArrayList<>();
dogs.add(new Dog(18));
dogs.add(new Dog(19));
/**
* 这个编译不能通过,因为T推断是Animal,得到的是Dog,Dog没有实现Comparable,所以不行
* mySort1(dogs);
* */ mySort2(animals);//ok
mySort2(dogs);//ok
} /**
* mySort1的类型参数是T extends Comparable<T>,他要求T必须实现Comparable
* @param list
* @param <T>
*/
public static <T extends Comparable<T>> void mySort1(List<T> list) {
Collections.sort(list);
System.out.println("mySort1 Success!");
} /**
* list中的元素必须是实现了Comparable接口的类或者其子类
* @param list
* @param <T>
*/
public static <T extends Comparable<? super T>> void mySort2(List<T> list) {
Collections.sort(list);
System.out.println("mySort2 Success!"); }
} class Animal implements Comparable<Animal> {
int age; public Animal(int age) {
this.age = age;
} @Override
public int compareTo(Animal o) {
return Integer.compare(this.age, o.age);
}
} /**
* Dog根本不能implements Comparable<Dog>,因为这样就会实现两个相同的接口
*/
class Dog extends Animal {
public Dog(int age) {
super(age);
}
}
如何理解<T extends Comparable<? super T>>的更多相关文章
- 如何理解<base href="<%=basePath%>" ---转载
原文链接http://316325524.blog.163.com/blog/static/6652052320111118111620897/ "base href " 今天在写 ...
- 如何理解<base href="<%=basePath%>"
原文链接http://316325524.blog.163.com/blog/static/6652052320111118111620897/ "base href " 今天在写 ...
- <base href="<%=basePath%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- JSP中<base href="<%=basePath%>">作用
通常在JSP页面开通有如下代码: <% String path = request.getContextPath(); String basePath = request.getScheme() ...
- JSP页面中 <base href="<%=basePath%>">
base标记是一个基链接标记,是一个单标记.用以改变文件中所有连结标记的参数内定值.它只能应用于标记<head>与</head>之间.你网页上的所有相对路径在链接时都将在前面加 ...
- 当使用了相对路径 <base href="<%= basePath %>" /> 后,全局都只能使用相对路径
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...
- JSP中<base href="<%=basePath%>">的作用
来源于:http://fanshuyao.iteye.com/blog/2097229 首先了解是什么是<base href=""> <base href=&qu ...
- [Angular2 Router] Understand the Angular 2 Base href Requirement
The <base href=”/”/> you define will determine how all other assets you plan on loading treat ...
- window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).prev().val();
window.location.href = basePath + "paper/deleteExpertComment.action?expertId="+$(this).pre ...
- 深入理解Oracle的并行操作-转载
转载:http://czmmiao.iteye.com/blog/1487568 并行(Parallel)和OLAP系统 并行的实现机制是:首先,Oracle会创建一个进程用于协调并行服务进程之间的信 ...
随机推荐
- 【31.58%】【codeforces 682D】Alyona and Strings
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- JSP页面中taglib的uri设置
今天遇到这样一个问题,使用JAVA做了个WEB应用,其中用到一个自定义标签.该标签的class文件与tld(tld文件中,uri定义为:http://wallimn.iteye.com/myfuncs ...
- WPF MVVM系列文章
网上搜到了MSDN Magazine上Laurent Bugnion的系列文章. 以下为关于WPF的优秀实践,很有必要阅读. 很吸引人的标题有: IOC Containers and MVVM Mes ...
- 网络故障模拟,cpu高压以及docker中的实现
利用tc进行丢包 通过网络丢包来模拟网络故障,是测试中一个重要的测试项目.这对服务来说可以测试其在网络故障时的异常处理的能力,对于服务的可靠性是一个相当严苛的测试. 网卡名为$netcard,丢包率为 ...
- Boost智能指针-基础知识
简单介绍 内存管理一直是 C++ 一个比較繁琐的问题,而智能指针却能够非常好的解决问题,在初始化时就已经预定了删除.排解了后顾之忧.1998年修订的第一版C++标准仅仅提供了一种智能指针:std::a ...
- WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...
- 机器学习: Python with Recurrent Neural Network
之前我们介绍了Recurrent neural network (RNN) 的原理: http://blog.csdn.net/matrix_space/article/details/5337404 ...
- python 教程 第五章、 函数
第五章. 函数 定义语句后面要加冒号 1) 定义函数 def sayHello(): print 'Hello World!' sayHello() 2) 变量作用域 LEGB原则 L本地 ...
- TestNg依靠先进的采用强制的依赖,并依赖序列的------TestNg依赖于特定的解释(两)
原创文章,版权所有所有,转载,归因:http://blog.csdn.net/wanghantong TestNg使用dependsOnGroups属性来进行依赖測试, 測试方法依赖于某个或某些方法, ...
- WPF编游戏系列 之九 物品清单再优化
原文:WPF编游戏系列 之九 物品清单再优化 在"第三篇"和"第四篇"中通过用户控件和数据绑定功能对物品清单进行一些优化减少了部分C#代码,但感觉 ...