泛型是一种代码级的有效复用手段,理解泛型的机制有利于更好的编写程序.
1) 泛型类
package generictest;
public class MyA {
public MyA()
{
}
public String Name;
public String ReturnName()
{
return Name+"hello";
}
}
package generictest;
public class MyB extends MyA implements MyInterface {
public MyB()
{
}
@Override
public String getXXXXName() {
// TODO Auto-generated method stub
return "From interface:"+this.Name;
}
}
package generictest;
public interface MyInterface {
public String getXXXXName();
}
package generictest;
import java.util.LinkedList;
import java.util.List;
import java.lang.reflect.*;
import java.lang.*;
public class MyGeneric<T extends MyA & MyInterface>
{
public LinkedList<T> objs = new LinkedList<T>();
public void AddT(T obj)
{
objs.add(obj);
}
//如果需要实例化参数类型,需要传递类类型参数.
public void AddT(String Name,Class<T> c) throws InstantiationException, IllegalAccessException
{
T obj=null;
//无法转换,编译通过,但会有错误
//Class<T> theC = (Class<T>) obj.getClass();
//T theO2 = theC.newInstance();
T theO2 = c.newInstance();
theO2.Name = Name;
System.out.println(theO2.getXXXXName());
AddT(theO2);
//
System.out.println(theO2.ReturnName());
}
}
2) 泛型方法及测试
package generictest; import java.util.LinkedList; public class GenericTest { public static void main(String args[]) { MyGeneric<MyB> theGeneric = new MyGeneric<MyB>(); try { theGeneric.AddT("ABC1",MyB.class); theGeneric.AddT("ABC2",MyB.class); theGeneric.AddT("ABC3",MyB.class); LinkedList<MyB> theObjs = theGeneric.objs; for(MyB theB : theObjs) { System.out.println(theB.Name); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } //下面语句有错,因为MyA不符合约束 //MyGeneric<MyA> theGeneric2 = new MyGeneric<MyA>(); System.out.println(""); } //泛型方法 public static <U extends MyA> U BuildInstance(Class<U> C) { try { return C.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
总结:
1) 泛型的约束不是必须的,但如果有类约束,类约束只能有一个,而且必须放在约束的第1位;
2)如果需要对泛型实例化,需要使用Class<T>参数明确传递到需要实例化的地方,而不能像后面语句那样直接实例化:T theT=new T();
这点上C#比较方便,只要泛型类型加上new()约束即可像实例化一般类那样对泛型类型实例化.T theT = new T();
3)<U<T>> 不合法,即泛型类型不能作为泛型类型;(在C#中是允许的)
4) 不能创建泛型数组(在C# 中是允许的)
MyGeneric<U>[] theA = new MyGeneric<U>[5];//是错误的写法.
5)泛型类型不能是基本类型,必须是类类型(继承于Object)(在C# 中是允许的)
MyGeneric<int> theA = new MyGeneric<int>();//是错误的写法.
6)泛类型并不能用instanceof测试来判断类型(在C# 中是允许的)
7)泛型类型不能用于异常捕获和异常抛出,也即如果泛型是异常类Exception的子类,并不能进行捕获和抛出.(在C# 中是允许的)
8)泛型类型不能出现在静态方法中.(在C# 中是允许的)
public class Singleton<T>
{
public static T getSingleInstance() // 错误
{
if (singleInstance == null) construct new instance of T
return singleInstance;
}
private static T singleInstance; //错误
}
9)泛型类型允许继承,但必须满足约束条件强化,即子类的约束条件不能弱于父类的约束条件.
public class MyGeneric2<T extends MyA & MyInterface & Runnable> extends MyGeneric<T> {
} 是合法的,但public class MyGeneric2<T extends MyA> extends MyGeneric<T> {
}是不合法的.
10) 如果B是A的子类,但MyGeneric2<B>和MyGeneric2<A>并无继承关系,是完全不同的两种类型.
11)泛型中使用通配符跟使用一般类型还是有区别的.(这种方式在C#中没有,也没必要)
Java的泛型机制和C#的泛型机制还是不一样的,C#的泛型机制更多的是模板作用,而Java的泛型采用的是底层替换的方式,本质上底层的泛型类型中的类型还是被替换成了Object,
只是帮你在做Cast而已.在运行时泛型类的类实例只有一份,而C# 中会根据不同的类型参数来实例化类实例,下面的测试可以证明:
public class GenericTest1<T> { public static int Count=0;//无论T为何类型,Count变量只有一份,说明泛型类只有一个类实例. public GenericTest1() { Count++; System.out.println(this.Count); } public String T2String(T c) { Count++; System.out.println(this.Count); return c.getClass().toString(); } }
public class GenericTest { public static void main(String args[]) { //定义类型参数为Integer泛型实例 GenericTest1<Integer> theInt = new GenericTest1<Integer>(); //输出theInt的类型为GenericTest1 System.out.println(theInt.getClass()); Integer theIntC= new Integer(1); System.out.println(theInt.T2String(theIntC)); //可以如下调用,说明泛型类属于共享性质,而非模板性质. System.out.println( GenericTest1.Count); GenericTest1<GenericTest> theGenericTest = new GenericTest1<GenericTest>(); //输出theGenericTest的类型为GenericTest1 System.out.println(theGenericTest.getClass()); GenericTest theTest = new GenericTest(); System.out.println(theGenericTest.T2String(theTest)); //可以如下调用,说明泛型类属于共享性质,而非模板性质. System.out.println( GenericTest1.Count); } }
从测试结果可以看出,Java的泛型和C#的泛型存在着本质上的区别,对于Java而言,无论类型参数是什么,其泛型类的类类型(例子中都为GenericTest1)都是一样的,类成员都是共享的,理解这点还是非常有用(Java中泛型做类型测试(instanceof)没有意义的根本原因).Java这样做的目的是为了向下兼容.而C#经常重构.当然,这无关两种处理方式的好坏,目的就是为了更好的利用这些特性.