package genericDemo5;
class GenericDemo5
{
public static void main(String[] args)
{
Tool<String> t=new Tool<String>();
t.show("java");
t.print(new Integer(4));
t.staticDemo(new Double(5.1));
Demo<String> d=new Demo<String>();
d.show("java");
Demo2 d1=new Demo2();
d1.show("java2");
}
}
//泛型類
class Tool<type>
{
//所用的泛型和類的相同
public void show(type t)
{
System.out.println(t);
}
//所用的方法和類的不同
public <T> void print(T t)
{
System.out.println(t);
}
//靜態方法,不能用類的泛型,只能自己定義
public static <W> void staticDemo(W t)
{
System.out.println(t);
}
}
//泛型接口
interface Inter<T>
{
public void show(T t);
}
interface Inter1<T>
{
public void show(T t);
}
//實現泛型接口
class Demo2 implements Inter1<String>
{
public void show(String t)
{
System.out.println(t);
}
}
//用泛型類指明類型
class Demo<T> implements Inter<T>
{
public void show(T t)
{
System.out.println("show"+t);
}
}
自定義泛型
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.