17.1. 泛型
17.1.1. 说明
增强了java的类型安全,可以在编译期间对容器内的对象进行类型检查,在运行期不必进行类型的转换。而在j2se5之前必须在运行期动态进行容器内对象的检查及转换,泛型是编译时概念,运行时没有泛型
减少含糊的容器,可以定义什么类型的数据放入容器
ArrayList aList = new ArrayList();
aList.add(new Integer(1));
// ...
Integer myInteger = aList.get(0);
我们可以看到,在这个简单的例子中,我们在定义aList的时候指明了它是一个直接受Integer类型的ArrayList,当我们调用aList.get(0)时,我们已经不再需要先显式的将结果转换成Integer,然后再赋值给myInteger了。而这一步在早先的Java版本中是必须的。也许你在想,在使用Collection时节约一些类型转换就是Java泛型的全部吗?远不止。单就这个例子而言,泛型至少还有一个更大的好处,那就是使用了泛型的容器类变得更加健壮:早先,Collection接口的get()和Iterator接口的next()方法都只能返回Object类型的结果,我们可以把这个结果强制转换成任何Object的子类,而不会有任何编译期的错误,但这显然很可能带来严重的运行期错误,因为在代码中确定从某个Collection中取出的是什么类型的对象完全是调用者自己说了算,而调用者也许并不清楚放进Collection的对象具体是什么类的;就算知道放进去的对象“应该”是什么类,也不能保证放到Collection的对象就一定是那个类的实例。现在有了泛型,只要我们定义的时候指明该Collection接受哪种类型的对象,编译器可以帮我们避免类似的问题溜到产品中。我们在实际工作中其实已经看到了太多的ClassCastException,不是吗?
17.1.2. 用法
声明及实例化泛型类:
HashMap hm = new HashMap();
编译类型的泛型和运行时类型的泛型一定要一致。没有多态。
不能使用原始类型
GenList nList = new GenList(); //编译错误
J2SE 5.0目前不支持原始类型作为类型参数(type parameter)
定义泛型接口:
public interface GenInterface {
void func(T t);
}
定义泛型类:
public class ArrayList { ... }
public class GenMap { ... }
例1:
public class MyList extends LinkedList
{
public void swap(int i, int j)
{
Element temp = this.get(i);
this.set(i, this.get(j));
this.set(j, temp);
}
public static void main(String[] args)
{
MyList list = new MyList();
list.add("hi");
list.add("andy");
System.out.println(list.get(0) + " " + list.get(1));
list.swap(0,1);
System.out.println(list.get(0) + " " + list.get(1));
}
}
17.1.3. 泛型的通配符"?"
package day16;
import java.util.*;
import static java.lang.System.*;
public class TestTemplate {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List l1=new ArrayList();
l1.add("abc");
l1.add("def");
List l2=new ArrayList();
l2.add(1.3);
l2.add(11);
List l3=new ArrayList();
l3.add(123);
l3.add(456);
// print(l1);
print(l2);
print(l3);
}
static void print(List extends Number> l){ //所有Number的子类
for(Object o:l){
out.println(o);
}
}
static void print(List super Number> l){ //所有Number的父类
for(Object o:l){
out.println(o);
}
}
}
"?"可以用来代替任何类型, 例如使用通配符来实现print方法。
public static void print(GenList> list) {})
17.1.4. 泛型方法的定义
void copyArrayToList(E[] os,List lst){
for(E o:os){
lst.add(o);
}
}
static void copyArrayToList(E[] os,List lst){
for(E o:os){
lst.add(o);
}
}
static void copyArrayToList(E[] os,List lst){
for(E o:os){
lst.add(o);
}
}
受限泛型是指类型参数的取值范围是受到限制的. extends关键字不仅仅可以用来声明类的继承关系, 也可以用来声明类型参数(type parameter)的受限关系.例如, 我们只需要一个存放数字的列表, 包括整数(Long, Integer, Short), 实数(Double, Float), 不能用来存放其他类型, 例如字符串(String), 也就是说, 要把类型参数T的取值泛型限制在Number极其子类中.在这种情况下, 我们就可以使用extends关键字把类型参数(type parameter)限制为数字
只能使用extends 不能使用 super ,只能向下,不能向上。
调用时用 > 定义时用
17.1.5. 泛型类的定义
类的静态方法不能使用泛型,因为泛型类是在创建对象的时候产生的。
class MyClass{
public void show(E a){
System.out.println(a);
}
public E get(){
return null;
}
}
受限泛型
class MyClass {
public void show(E a){
}
}
17.1.6. 泛型与异常
类型参数在catch块中不允许出现,但是能用在方法的throws之后。例:
import java.io.*;
interface Executor {
void execute() throws E;
}
public class GenericExceptionTest {
public static void main(String args[]) {
try {
Executor e = new Executor() {
public void execute() throws IOException{
// code here that may throw an
// IOException or a subtype of
// IOException
}
};
e.execute();
} catch(IOException ioe) {
System.out.println("IOException: " + ioe);
ioe.printStackTrace();
}
}
}
17.1.7. 泛型的一些局限型
不能实例化泛型
T t = new T(); //error
不能实例化泛型类型的数组
T[] ts= new T[10]; //编译错误
不能实例化泛型参数数
Pair[] table = new Pair(10); // ERROR
类的静态变量不能声明为类型参数类型
public class GenClass {
private static T t; //编译错误
}
泛型类不能继承自Throwable以及其子类
public GenExpection extends Exception{} //编译错误
不能用于基础类型int等
Pair //error
Pair //right
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23155850/viewspace-663760/
,如需转载,请注明出处,否则将追究法律责任。