1泛型类:public class FanXingLeiDemo {//这里是泛型,可以传任意的参数 private T obj; public T getObj() { return obj; } public void setObj(T obj) { this.obj = obj; } }实现:public class FanXingMain { public static void main(String[] args) { FanXingLeiDemo fxd = new FanXingLeiDemo(); fxd.setObj('hahaha'); System.out.println(fxd.getObj()); }}
2泛型方法:public class FanXingMethod { public void show(T t) { System.out.println(t); }}实现:public class FangXingMethodMain { public static void main(String[] args) { FanXingMethod fxm = new FanXingMethod(); fxm.show(100); fxm.show('shshh'); fxm.show(true); }}
3泛型接口:public interface Inter { public abstract void show(T t);}实现接口:public class InterImpl implements Inter { @Override public void show(T t) { // TODO 自动生成的方法存根 System.out.println(t); }}实例化:public class InterMain { public static void main(String[] args) { Inter in = new InterImpl(); in.show('hahah'); }}