自定义泛型

来源:互联网 发布:阴阳师ios网络连接出错 编辑:程序博客网 时间:2024/04/28 00:47
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);}}

0 0
原创粉丝点击