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);
}
}
自定义泛型
最新推荐文章于 2022-01-15 16:44:33 发布