关于java泛型的使用方式。。。。
转自:http://onewebsql.com/blog/generics-extends-super
以下基本够用了
Today we continue our mini-series on Java Generics. In previous posts we have investigated
- how to crash your IDE using Java wildcards,
- what beginners find surprising when creating parametrized objects, and
- what is the difference between Java arrays and generic lists.
Java type parameters are used as type placeholders.
|
1
|
public class List<X> { } |
A List<X> is a container for X objects, and X can be instantiated with any class: you can haveList<Object>, List<String>, and List<Number>.
Introducing bounds: extends
You often want to restrict the set of types that can be used in instantiation. If you create a class Garage, you want it to hold only Vehicle objects.
The syntax you use in Java is like this:
|
1
|
public class Garage<X extends Vehicle> { } |
Every time you instantiate the Garage, the type parameter has to be a subclass of Vehicle.
|
1
2
3
|
class Car extends Vehicle { }class Motorcycle extends Vehicle { }class Fruit extends Object { } |
Thus Garage<Car> and Garage<Motorcycle> are OK but Garage<Fruit> is not OK.
You can specify more than one bound with extends:
|
1
2
3
4
|
class Vehicle { }interface PassengerVehicle { }interface MotorVehicle { }class ParkingGarage<X extends Vehicle & MotorVehicle & PassengerVehicle> |
You can specify at most one class in the bound (obviously, as you can only inherit from one class in Java) and as many interfaces as you want.
The bound can refer to the typing parameter:
|
1
|
class BST<X extends Comparable<X>> {} |
BST class can only be instantiated with classes X which implement the Comparable<X>interface.
Bounds in method parameters
Java method can be parametrized, too. The syntax is as follows:
|
1
|
<T> T getRandomElement(List<T> list) {} |
As with class definitions, you often want to restrict the type parameter in the method. A method which takes a list of Vehicles and returns the fastest vehicle in the list can have the following type.
|
1
|
<T extends Vehicle> T getFastest(List<T> list) { } |
You can pass as argument a list of any vehicles. List<Car> is OK, List<Motorcycle> is OK, List<Vehicle> is OK, too. List<Number> is not OK.
Note that the following declaration wouldn't do the trick.
|
1
|
Vehicle getFastest2(List<Vehicle> list) { } |
The argument to the method getFastest2 has to be exactly a List<Vehicle>, and not aList<Car>, because List<Car> is not a subtype of List<Vehicle>,
Wilcards
Take a look at the following declaration.
|
1
|
<T extends Vehicle> int totalFuel(List<T> list) { } |
The parameter T occurs only once in the method signature, in an argument. You can imagine that the method body does not use the name T either. In this case you can use an alternative syntax, called wildcards, denoted with ?:
|
1
|
int totalFuel(List<? extends Vehicle> list) { } |
The two signatures for totalFuel are equivalent. The meaning of <? extends Vehicle> is: I don't care what the type parameter is, as long as it is a subclass of Vehicle.
Introducing bounds: super
There is also dual bound, called super. As you guess it is used to denote that you can pass only superclasses of the bound. There are some differences between extends and super, though.
You can't use super in class declaration
The super bound is not allowed in class definition.
|
1
2
|
//this code does not compile !class Forbidden<X super Vehicle> { } |
Why? Because such construction doesn't make sense. For example, you can't erase the type parameter with Vehicle because the class Forbidden could be instantiated with Object. So you have to erase type parameters to Object anyway. If think about class Forbidden<Object>, it can take any value in place of X, not only superclasses of Vehicle. There's no point in using super bound, it wouldn't get us anything. Thus it is not allowed.
Wildcards
The syntax for wildcards is also similar to extends:
|
1
|
int totalValue(Valuer<? super Vehicle> valuer) |
The method has to take a comparator which is able to compare Vehicles. If it comparesObjects as well, that's fine too.
When to use extends and super
Wildcards are most useful in method parameters. They allow for the necessary flexibility in method interfaces.
People are often confused when to use extends and when to use super bounds. The rule of thumb is the get-put principle. If you get something from a parametrized container, useextends.
|
1
2
3
4
5
6
7
|
int totalFuel(List<? extends Vehicle> list) { int total = 0; for(Vehicle v : list) { total += v.getFuel(); } return total;} |
The method totalFuel gets Vehicles from the list, asks them about how much fuel they have, and computes the total.
If you put objects into a parametrized container, use super.
|
1
2
3
4
5
6
7
|
int totalValue(Valuer<? super Vehicle> valuer) { int total = 0; for(Vehicle v : vehicles) { total += valuer.evaluate(v); } return total;} |
The method totalValue puts Vehicles into the Valuer.
It's useful to know that extends bound is much more common than super.
One more tip: if you are intimidated by wildcards (which is natural in the beginning), try to write the explicitly parametrized version first. In typical usage the two versions are equivalent. Eventually, you'll figure out when you can get rid of type parameters and use wildcards.
关于java泛型的使用方式。。。。的更多相关文章
- 用了这么多年的 Java 泛型,你对它到底有多了解?
作为一个 Java 程序员,日常编程早就离不开泛型.泛型自从 JDK1.5 引进之后,真的非常提高生产力.一个简单的泛型 T,寥寥几行代码, 就可以让我们在使用过程中动态替换成任何想要的类型,再也不用 ...
- Java泛型学习---第二篇
泛型学习第一篇 1.泛型之擦拭法 泛型是一种类似"模板代码"的技术,不同语言的泛型实现方式不一定相同. Java语言的泛型实现方式是擦拭法(Type Erasure). 所谓擦拭法 ...
- Java中泛型的详细解析,深入分析泛型的使用方式
泛型的基本概念 泛型: 参数化类型 参数: 定义方法时有形参 调用方法时传递实参 参数化类型: 将类型由原来的具体的类型参数化,类似方法中的变量参数 类型定义成参数形式, 可以称为类型形参 在使用或者 ...
- java泛型基础
泛型是Java SE 1.5的新特性, 泛型的本质是参数化类型, 也就是说所操作的数据类型被指定为一个参数. 这种参数类型可以用在类.接口和方法的创建中, 分别称为泛型类.泛型接口.泛型方法. Ja ...
- 使用java泛型设计通用方法
泛型是Java SE 1.5的新特性, 泛型的本质是参数化类型, 也就是说所操作的数据类型被指定为一个参数. 因此我们可以利用泛型和反射来设计一些通用方法. 现在有2张表, 一张user表和一张stu ...
- Java泛型
什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个 ...
- 初识java泛型
1 协变数组类型(covariant array type) 数组的协变性: if A IS-A B then A[] IS-A B[] 也就是说,java中的数组兼容,一个类型的数组兼容他的子类类型 ...
- 【Java心得总结四】Java泛型下——万恶的擦除
一.万恶的擦除 我在自己总结的[Java心得总结三]Java泛型上——初识泛型这篇博文中提到了Java中对泛型擦除的问题,考虑下面代码: import java.util.*; public clas ...
- Java深度历险(五)——Java泛型
作者 成富 发布于 2011年3月3日 | 注意:QCon全球软件开发大会(北京)2016年4月21-23日,了解更多详情!17 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件 ...
随机推荐
- HDU 1936 区间贪心
/* *区间贪心.前几天刚做了POJ 1328 ...思路完全相同... *最多有100个表情,100行文字.遍历寻找每个表情的所在区间.时间复杂度大约在10^5 ~ 10^6 可以接受. *然后对每 ...
- jsp jstl的使用
1)下载jstl.jar和standard.jar文件,然后将其拷贝到tomcat的lib目录下. 具体的下载地址:http://mirrors.ccs.neu.edu/Apache/dist/jak ...
- o(1)取b > a,且b的二进制中1的个数等于a二进制中1的个数,且使b最小
给你一个uint32 a,让你找到另一个uint32 b,使b > a,且b的二进制中1的个数等于a二进制中1的个数.且使b最小.(数据保证可出) 1 因为1的个数不变,所以必然大于n+lowb ...
- 玩转X-CTR100 l 平台-4WD智能小车
我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍使用X-CTR100控制器搭建4WD智能 ...
- 20165210 Java第六周学习总结
20165210 Java第六周学习总结 教材学习内容 第八章学习总结 String类: 构造String对象: 1. 常量对象 2. String对象 3. 引用String常量 字符串的并置: S ...
- 入门级:理解FAT32文件系统(转载翻译)
FAT(File Allocation Table ) 这个网页的目的是帮助你理解怎么样在微软FAT32文件系统下取得数据,处理的硬盘的大小通常在500M到几百G之间.FAT是一个相对简单和纯净的文件 ...
- HandBrake 开源视频转码器、编码转换器、格式转换器
HandBrake 开源视频转码器.编码转换器.格式转换器 点击下图进入官网下载页面:https://handbrake.fr/downloads.php macOS 下可能会阻止安装! 其实也不是安 ...
- uva1482:Playing With Stones (SG函数)
题意:有N堆石子,每次可以取一堆的不超过半数的石子,没有可取的为输. 思路:假设只有一堆,手推出来,数量x可以表示为2^p-1形式的必输. 但是没什么用,因为最后要的不是0和1,而是SG函数:所以必输 ...
- BZOJ4310: 跳蚤 【后缀数组+二分】
Description 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究.首先,他会把串 分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典 ...
- File、Directory、Path
File.Directory.Path https://blog.csdn.net/xiaouncle/article/details/52050577 File.Directory.Path是实际开 ...