//使用泛型
class Tools<T>
{
private T obj;
public void setObject(T obj)
{
this.obj = obj;
}
public T getObject()
{
return this.obj;
}
}class Test<E>
{
//泛型用在方法上
//当类中的某个方法所使用的类型只有在类上的类型确定了才能确定时,就在方法上使用
//泛型
//否则不用泛型
public void func(E e)
{
System.out.println(e);
}
//该方法单独使用泛型: 方法使用的类型不确定,而且什么类型都行
//这个 方法被调用时,类型才确定
public <T> void show(T e)
{
System.out.println(e);
}
//静态是随着类的加载而加载,这是不存在类型,所以静态方法只能自己定义泛型
public static <W> void function(W e)//Test.function()
{
System.out.println(e);
}
public void ff(int a,int b)
{
System.out.println(a+b);
}
}//泛型用在接口上
interface inter<T>
{
public void show(T t);
}
class Test implements inter<String>
{
public void show(String t)
{
}
}
class Demo<E> implements inter<E>
{
public void show(E t)
{
System.out.println(t);
}
}
class Demo9
{
public static void main(String[] args)
{
Demo<String> d = new Demo<String>();
d.show(99);// error
}
}
原文:http://blog.csdn.net/love_javc_you/article/details/38306585