一、什么是Generics?
Generics可以称之为参数类型(parameterized types),由编译器来验证从客户端将一种类型传送给某一对象的机制。如Java.util.ArrayList,编译器可以用Generics来保证类型安全。
在我们深入了解Generics之前,我们先来看一看当前的java 集合框架(Collection)。在j2SE1.4中所有集合的Root Interface是Collection。
Collections example without genericity: Example 1
protected void collectionsExample() { ArrayList list = new ArrayList(); list.add(new String("test string")); list.add(new Integer(9)); // purposely placed here to create a runtime ClassCastException inspectCollection(list); } protected void inspectCollection(Collection aCollection) { Iterator i = aCollection.iterator(); while (i.hasNext()) { String element = (String) i.next(); } }
以上的样例程序包含的两个方法,collectionExample方法建立了一个简单的集合类型ArrayList,并在ArrayList中增加了一个String和一个Integer对象.而在inspecCollection方法中,我们迭代这个ArrayList用String进行Cast。我们看第二个方法,就出现了一个问题,Collection在内部用的是Object,而我们要取出Collection中的对象时,需要进行Cast,那么开发者必需用实际的类型进行Cast,像这种向下造型,编译器无法进行检查,如此一来我们就要冒在代码在运行抛出ClassCastException的危险。我们看inspecCollection方法,编译时没有问题,但在运行时就会抛出ClassCastException异常。所以我们一定要远离这个重大的运行时错误。
二、使用Generics
从上一章节中的CassCastException这种异常,我们期望在代码编译时就能够捕捉到,下面我们使用范型修改上一章的样例程序。
//Example 2 protected void collectionsExample() { ArrayList<String> list = new ArrayList<String>(); list.add(new String("test string")); // list.add(new Integer(9)); this no longer compiles inspectCollection(list); } protected void inspectCollection(Collection<String> aCollection) { Iterator<String> i = aCollection.iterator(); while(i.hasNext()) { String element = i.next(); } }
从上面第2行我们在创建ArrayList时使用了新语法,在JDK1.5中所有的Collection都加入了Generics的声明。例:
//Example 3 public class ArrayList<E> extends AbstractList<E> { // details omitted... public void add(E element) { details omitted } public Iterator<E> iterator() { // details omitted } }
这个E是一个类型变量,并没有对它进行具体类型的定义,它只是在定义ArrayList时的类型占位符,在Example 2中的我们在定义ArrayList的实例时用String绑定在E上,当我们用add(E element)方法向ArrayList中增加对象时,那么就像下面的写法一样: public void add(String element);因为在ArrayList所有方法都会用String来替代E,无论是方法的参数还是返回值。这时我们在看Example 2中的第四行,编译就会反映出编译错误。
所以在java中增加Generics主要的目的是为了增加类型安全。
通过上面的简单的例子我们看到使用Generics的好处有:
1、在类型没有变化时,Collection是类型安全的。
2、内在的类型转换优于在外部的人工造型。
3、使Java 接口更加强壮,因为它增加了类型。
4、类型的匹配错误在编译阶段就可以捕捉到,而不是在代码运行时。
受约束类型变量
虽然许多Class被设计成Generics,但类型变量可以是受限的
public class C1<T extends Number> { }
public class C2<T extends Person & Comparable> { }
第一个T变量必须继承Number,第二个T必须继承Person和实现Comparable。
三、Generics 方法
像Generics类一样,方法和构造函数也可以有类型参数。方法的参数的返回值都可以有类型参数,进行Generics。
//Example 4 public <T extends Comparable> T max(T t1, T t2) { if (t1.compareTo(t2) > 0) return t1; else return t2; }
这里,max方法的参数类型为单一的T类型,而T类型继承了Comparable,max的参数和返回值都有相同的超类。下面的Example 5显示了max方法的几个约束。
//Example 5 Integer iresult = max(new Integer(100), new Integer(200)); String sresult = max("AA", "BB"); Number nresult = max(new Integer(100), "AAA"); // does not compile
在Example 5第1行参数都为Integer,所以返回值也是Integer,注意返回值没有进行造型。
在Example 5第2行参数都为String,所以返回值也是String,注意返回值没有进行造型。以上都调用了同一个方法。
在Example 5第3行产生以下编译错误:
Example.java:10: incompatible types found : java.lang.Object&java.io.Serializable&java.lang.Comparable<?> required: java.lang.Number Number nresult = max(new Integer(100), "AAA");
这个错误发生是因为编译器无法确定返回值类型,因为String和Integer都有相同的超类Object,注意就算我们修正了第三行,这行代码在运行仍然会报错,因为比较了不同的对象。
四、向下兼容
任何一个新的特色在新的JDK版本中出来后,我们首先关心的是如何于以前编写的代码兼容。也就是说我们编写的Example 1程序不需要任何的改变就可以运行,但是编译器会给出一个"ROW TYPE"的警告。在JDK1.4中编写的代码如何在JVM1.5中完全兼容运行,我们要人工进行一个:Type erasure处理过程。
五、通配符
//Example 6 List<String> stringList = new ArrayList<String>(); //1 List<Object> objectList = stringList ;//2 objectList .add(new Object()); // 3 String s = stringList .get(0);//4
乍一看,Example 6是正确的。但stringList本意是存放String类型的ArrayList,而objectList中可以存入任何对象,当在第3行进行处理时,stringList也就无法保证是String类型的ArrayList,此时编译器不允许这样的事出现,所以第3行将无法编译。
//Example 7 void printCollection(Collection<Object> c) { for (Object e : c) { System.out.println(e); } }
Example 7的本意是打印所有Collection的对象,但是正如Example 6所说的,编译会报错,此时就可以用通配符“?”来修改Example 7
//Example 8 void printCollection(Collection<?> c) { for (Object e : c) { System.out.println(e); } }
Example 8中所有Collection类型就可以方便的打印了。
有界通配符 <T extends Number>(上界),<T super Number>(下界)。
六、创建自己的范型
以下代码来自:http://www.java2s.com/ExampleCode/Language-Basics
1、一个参数的Generics
//Example 9(没有使用范型) class NonGen { Object ob; // ob is now of type Object // Pass the constructor a reference to // an object of type Object NonGen(Object o) { ob = o; } // Return type Object. Object getob() { return ob; } // Show type of ob. void showType() { System.out.println("Type of ob is " + ob.getClass().getName()); } } // Demonstrate the non-generic class. public class NonGenDemo { public static void main(String args[]) { NonGen iOb; // Create NonGen Object and store // an Integer in it. Autoboxing still occurs. iOb = new NonGen(88); // Show the type of data used by iOb. iOb.showType(); // Get the value of iOb. // This time, a cast is necessary. int v = (Integer) iOb.getob(); System.out.println("value: " + v); System.out.println(); // Create another NonGen object and // store a String in it. NonGen strOb = new NonGen("Non-Generics Test"); // Show the type of data used by strOb. strOb.showType(); // Get the value of strOb. // Again, notice that a cast is necessary. String str = (String) strOb.getob(); System.out.println("value: " + str); // This compiles, but is conceptually wrong! iOb = strOb; v = (Integer) iOb.getob(); // runtime error! } }
再看下面的:
// Example 10(使用范型) class Example1<T>{ private T t; Example1(T o){ this.t=o; } T getOb(){ return t; } void ShowObject(){ System.out.println("对象的类型是:"+t.getClass().getName()); } } public class GenericsExample1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Example1<Integer> examplei=new Example1<Integer>(100); examplei.ShowObject(); System.out.println("对象是:"+examplei.getOb()); Example1<String> examples=new Example1<String>("Bill"); examples.ShowObject(); System.out.println("对象是:"+examples.getOb()); } }
我们来看Example 9没有使用范型,所以我们需要进行造型,而Example 10我们不需要任何的造型
七、二个参数的Generics
//Example 11 class TwoGen<T, V> { T ob1; V ob2; // Pass the constructor a reference to // an object of type T. TwoGen(T o1, V o2) { ob1 = o1; ob2 = o2; } // Show types of T and V. void showTypes() { System.out.println("Type of T is " + ob1.getClass().getName()); System.out.println("Type of V is " + ob2.getClass().getName()); } T getob1() { return ob1; } V getob2() { return ob2; } } public class GenericsExampleByTwoParam { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub TwoGen<Integer, String> tgObj = new TwoGen<Integer, String>(88, "Generics"); // Show the types. tgObj.showTypes(); // Obtain and show values. int v = tgObj.getob1(); System.out.println("value: " + v); String str = tgObj.getob2(); System.out.println("value: " + str); } }
八、Generics的Hierarchy
//Example 12 class Stats<T extends Number> { T[] nums; // array of Number or subclass // Pass the constructor a reference to // an array of type Number or subclass. Stats(T[] o) { nums = o; } // Return type double in all cases. double average() { double sum = 0.0; for(int i=0; i < nums.length; i++) sum += nums[i].doubleValue(); return sum / nums.length; } } public class GenericsExampleByHierarchy { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Integer inums[] = { 1, 2, 3, 4, 5 }; Stats<Integer> iob = new Stats<Integer>(inums); double v = iob.average(); System.out.println("iob average is " + v); Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 }; Stats<Double> dob = new Stats<Double>(dnums); double w = dob.average(); System.out.println("dob average is " + w); // This won't compile because String is not a // subclass of Number. // String strs[] = { "1", "2", "3", "4", "5" }; // Stats<String> strob = new Stats<String>(strs); // double x = strob.average(); // System.out.println("strob average is " + v); } }
九、使用通配符
//Example 14 class StatsWildCard<T extends Number> { T[] nums; // array of Number or subclass // Pass the constructor a reference to // an array of type Number or subclass. StatsWildCard(T[] o) { nums = o; } // Return type double in all cases. double average() { double sum = 0.0; for (int i = 0; i < nums.length; i++) sum += nums[i].doubleValue(); return sum / nums.length; } // Determine if two averages are the same. // Notice the use of the wildcard. boolean sameAvg(StatsWildCard<?> ob) { if (average() == ob.average()) return true; return false; } } public class GenericsExampleByWildcard { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Integer inums[] = { 1, 2, 3, 4, 5 }; StatsWildCard<Integer> iob = new StatsWildCard<Integer>(inums); double v = iob.average(); System.out.println("iob average is " + v); Double dnums[] = { 1.1, 2.2, 3.3, 4.4, 5.5 }; StatsWildCard<Double> dob = new StatsWildCard<Double>(dnums); double w = dob.average(); System.out.println("dob average is " + w); Float fnums[] = { 1.0F, 2.0F, 3.0F, 4.0F, 5.0F }; StatsWildCard<Float> fob = new StatsWildCard<Float>(fnums); double x = fob.average(); System.out.println("fob average is " + x); // See which arrays have same average. System.out.print("Averages of iob and dob "); if (iob.sameAvg(dob)) System.out.println("are the same."); else System.out.println("differ."); System.out.print("Averages of iob and fob "); if (iob.sameAvg(fob)) System.out.println("are the same."); else System.out.println("differ."); } }
十、使用边界通配符
//Example 15 class TwoD { int x, y; TwoD(int a, int b) { x = a; y = b; } } // Three-dimensional coordinates. class ThreeD extends TwoD { int z; ThreeD(int a, int b, int c) { super(a, b); z = c; } } // Four-dimensional coordinates. class FourD extends ThreeD { int t; FourD(int a, int b, int c, int d) { super(a, b, c); t = d; } } // This class holds an array of coordinate objects. class Coords<T extends TwoD> { T[] coords; Coords(T[] o) { coords = o; } } // Demonstrate a bounded wildcard. public class BoundedWildcard { static void showXY(Coords<?> c) { System.out.println("X Y Coordinates:"); for(int i=0; i < c.coords.length; i++) System.out.println(c.coords[i].x + " " + c.coords[i].y); System.out.println(); } static void showXYZ(Coords<? extends ThreeD> c) { System.out.println("X Y Z Coordinates:"); for(int i=0; i < c.coords.length; i++) System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z); System.out.println(); } static void showAll(Coords<? extends FourD> c) { System.out.println("X Y Z T Coordinates:"); for(int i=0; i < c.coords.length; i++) System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z + " " + c.coords[i].t); System.out.println(); } public static void main(String args[]) { TwoD td[] = { new TwoD(0, 0), new TwoD(7, 9), new TwoD(18, 4), new TwoD(-1, -23) }; Coords<TwoD> tdlocs = new Coords<TwoD>(td); System.out.println("Contents of tdlocs."); showXY(tdlocs); // OK, is a TwoD // showXYZ(tdlocs); // Error, not a ThreeD // showAll(tdlocs); // Erorr, not a FourD // Now, create some FourD objects. FourD fd[] = { new FourD(1, 2, 3, 4), new FourD(6, 8, 14, 8), new FourD(22, 9, 4, 9), new FourD(3, -2, -23, 17) }; Coords<FourD> fdlocs = new Coords<FourD>(fd); System.out.println("Contents of fdlocs."); // These are all OK. showXY(fdlocs); showXYZ(fdlocs); showAll(fdlocs); } }
十一、ArrayList的Generics
//Example 16 public class ArrayListGenericDemo { public static void main(String[] args) { ArrayList<String> data = new ArrayList<String>(); data.add("hello"); data.add("goodbye"); // data.add(new Date()); This won't compile! Iterator<String> it = data.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } } }
十二、HashMap的Generics
//Example 17 public class HashDemoGeneric { public static void main(String[] args) { HashMap<Integer,String> map = new HashMap<Integer,String>(); map.put(1, "Ian"); map.put(42, "Scott"); map.put(123, "Somebody else"); String name = map.get(42); System.out.println(name); } }
十三、接口的Generics
//Example 18 interface MinMax<T extends Comparable<T>> { T min(); T max(); } // Now, implement MinMax class MyClass<T extends Comparable<T>> implements MinMax<T> { T[] vals; MyClass(T[] o) { vals = o; } // Return the minimum value in vals. public T min() { T v = vals[0]; for(int i=1; i < vals.length; i++) if(vals[i].compareTo(v) < 0) v = vals[i]; return v; } // Return the maximum value in vals. public T max() { T v = vals[0]; for(int i=1; i < vals.length; i++) if(vals[i].compareTo(v) > 0) v = vals[i]; return v; } } public class GenIFDemo { public static void main(String args[]) { Integer inums[] = {3, 6, 2, 8, 6 }; Character chs[] = {'b', 'r', 'p', 'w' }; MyClass<Integer> iob = new MyClass<Integer>(inums); MyClass<Character> cob = new MyClass<Character>(chs); System.out.println("Max value in inums: " + iob.max()); System.out.println("Min value in inums: " + iob.min()); System.out.println("Max value in chs: " + cob.max()); System.out.println("Min value in chs: " + cob.min()); } }
十四、Exception的Generics
//Example 20 interface Executor<E extends Exception> { void execute() throws E; } public class GenericExceptionTest { public static void main(String args[]) { try { Executor<IOException> e = new Executor<IOException>() { 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(); } } }
十五、两种写法
package com.test; public class EntityDao1 { public <T> void add(T t){ //查询实体的代码 } public <T,ID> T get(ID id){ //.保存实体的代码 return null; } }
范型一般用于方法的参数或者方法的返回值,上面的写法,我们要使范型有效,就须在方法的返回类型前加入强制范型转换。其中,add(T t)的参数用了范型,它的返回值是void型,就在void 前用强制类型转换,即加上<T>,强制转换成范型的形式,这样就不会报错了。而T get(ID id),由于它的参数和返回类型都用了范型,故要在返回类型T前强制转换,即<T,ID>。
package com.test; public class EntityDao2<T,ID> { public void add(T t){ //..保存实体的代码 } public T get(ID id){ //.查询实体的代码 return null; } }
这种形式,是把范型声明放在类中了,就不需每个方法都写强制类型转换。
看实际需要,哪种方法方便就用哪种吧!范型给Java编程带来了许多方便,好好利用,会达到事半功倍的效果。
sdsdfsd
相关推荐
Java 范型Java 范型
### Java范型攻略篇:深度解析与应用 #### 一、引言:Java范型的引入 在Java的发展历程中,范型(Generics)的引入标志着语言设计上的一个重要里程碑。自Java 1.5发布以来,范型成为了Java语言的重要特性之一,极...
Java 泛型是一种在编程中实现强类型检查和减少冗余类型转换的机制,它是在JDK 1.5版本中引入的。泛型的主要目标是提高代码的类型安全性、可读性和重用性,避免在运行时出现类型转换异常。 1. **泛型的基本概念** -...
Java泛型是Java 5版本引入的一个重要特性,极大地增强了代码的类型安全性和效率。泛型允许我们在编写类、接口和方法时指定一种或多种类型参数,使得代码能够处理多种不同类型的对象,同时在编译时进行严格的类型检查...
Java泛型是Java 5版本引入的一个重要特性,极大地增强了代码的类型安全性和可读性。泛型允许我们在编写代码时指定容器(如List、Set、Map等集合类)能够存储的数据类型,从而避免了不必要的类型转换,并在编译时期就...
在Java 1.5中引入的泛型(Generics)是编程语言的重大改进,它允许开发者在定义类、接口和方法时指定参数类型,从而提供了更强的类型检查和类型安全性。泛型的主要目标是消除强制类型转换,防止在运行时出现类型错误...
泛型是Java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数。这种参数类型可以用在类、接口和方法的创建中,分别称为泛型类、泛型接口、泛型方法。 Java语言引入泛型的好处是...
在Java编程语言中,泛型(Generics)是一种强大的特性,它允许我们在编写代码时指定容器(如集合)可以存储的数据类型。这提高了代码的安全性和效率,因为编译器可以在编译时检查类型,避免了运行时...
Java 泛型是一种强大的语言特性,它在J2SE 5.0中引入,极大地提升了代码的类型安全性和效率。泛型允许我们在编写类、接口和方法时指定一种或多种类型参数,使得代码能够处理多种数据类型,同时避免了运行时的类型...
Java程序设计范型和枚举是Java编程中的关键概念,它们极大地增强了代码的类型安全性和重用性。本文将深入探讨这两个主题。 首先,我们来看什么是范型(Generics)。范型是Java SE 5.0引入的一个特性,它允许在类、...
在本文中,我们将深入探讨如何使用Java的Socket编程来实现一个简单的基于P2P(Peer-to-Peer)范型的即时聊天系统。P2P网络架构允许每个节点既是客户端也是服务器,这种模式使得数据传输更加分散,提高了系统的可扩展...
Java 实现泛型List的源码,基本实现了List接口的全部所有方法。欢迎大家发表自己的观点和建议。
范型参考 (1).java
范型参考 (2).java
《C++多范型设计》是一本深入探讨C++编程语言中模板技术的专著,由知名软件工程师James O. Coplien撰写,并由鄢爱兰、周辉等翻译成中文版,ISBN号为9787508318240。这本书的核心主题是C++中的泛型编程,它是C++编程...
参考:我眼中的Java-Type体系(1) 我眼中的Java-Type体系(2) 秒懂Java类型(Type)系统 Java 运行时如何获取泛型参数的类型 Java类型Type 之 ParameterizedType,GenericArrayType,TypeVariabl,WildcardType 从实现...
Java范型是Java编程语言中的一个重要特性,它允许在类、接口和方法中使用类型参数,从而提高了代码的重用性和安全性。范型在Java中引入的主要目标是增强类型安全,减少强制类型转换,并帮助开发者编写更清晰、更易于...
本文提出的基于消息传递范型和客户机/服务器(Client/Server,简称C/S)范型双范型的主数据管理机制,能够有效解决MDM面临的问题。 消息传递范型是计算机科学中的一个基本概念,用于描述进程间通信的方式。在这范型...
《范型程序设计与 STL》是一本深入探讨C++编程中的关键概念和技术的书籍,主要聚焦于范型(Generic Programming)和标准模板库(Standard Template Library,简称STL)。范型编程是一种强大的软件开发方法,它允许...
本例子说明演示了Java范型使用的动机、范型类的使用、范型方法的使用,以及范型的缺陷:类型擦除(type erasure).因为,现在Java的反射机制不能确定集合中的对象类型! 在实际应用中,如果能够灵活应用范型和反射...