java中的泛型总结
要我直接说出泛型是个what我还真讲不出来,这里先由一道问题引入:
定义一个坐标点类,要求能保存各种类型的数据,如:整形,浮点型,和字符串类型
既然变量类型起先不确定,那么很容易想到就是用所有类型的父类,也就是Object类来代替
不废话了,用代码来体现
实例1:用Object来实现不确定的数据类型输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | //这是定义的坐标点类 class Point { private Object x; private Object y; //用Object来表示不确定的类型 public Point(Object x, Object y) { this .setX(x); this .setY(y); } public void setX(Object x) { this .x = x; } public Object getX() { return x; } public void setY(Object y) { this .y = y; } public Object getY() { return y; } } //测试类 public class Demo { public static void main(String[] args) { System.out.println( "用浮点数表示坐标: " ); Point p = new Point( 12.23 , 23.21 ); //这里把Object类转为Double类,然后自动拆箱,下面两种一样 System.out.println( "X的坐标 " + (Double)p.getX()); System.out.println( "Y的坐标 " + (Double)p.getY()); System.out.println(); System.out.println( "用整数表示坐标: " ); Point p2 = new Point( 12 , 23 ); System.out.println( "X的坐标 " + (Integer)p2.getX()); System.out.println( "Y的坐标 " + (Integer)p2.getY()); System.out.println(); System.out.println( "用字符串表示坐标: " ); Point p3 = new Point( "北纬29度" , "东经113度" ); System.out.println( "X的坐标 " + (String)p3.getX()); System.out.println( "Y的坐标 " + (String)p3.getY()); } } |
这样就可以代入不同类型数据了,但你别忘了,此时的数据还是Object型,也就是所有类型的父类
你必须清醒的明白自己传入的是什么类型,然后将其做向下转型处理才能使用
虽然这样做满足了需求,不过却隐含了一个不安全因素,为什么说是隐含呢?
比如我们用new Point(12.23,"北纬29度")来构造一个Point对象
然后都用(Double)将其向下转型,会产生什么结果?
没错,编译会通过,但是一旦运行则会发生类型转换异常
要避免类转换异常也很简单,把Object声明换成固定类型声明(如:String x,String y)即可,这样编译时就会报错
然后你就可以寻找出错的地方进行修改
不过如此一来,我们就满足不了需求了
为了达到不存在安全隐患和代入各种数据类型的目的,那些牛人们在JDK1.5当中引入了泛型这一概念
我们来看看如何用泛型改写上面的代码
实例2:泛型类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | class Point<T> { //这里用T来表示不确定的类型 private T x; private T y; public Point(T x, T y) { this .setX(x); this .setY(y); } public T getX() { return x; } public void setX(T x) { this .x = x; } public T getY() { return y; } public void setY(T y) { this .y = y; } } public class Demo { public static void main(String[] args) { System.out.println( "用浮点数表示坐标: " ); //用泛型改写后,使用数据无需再做向下转型处理 Point<Double> p = new Point<Double>( 12.23 , 23.21 ); System.out.println( "X的坐标 " + p.getX()); System.out.println( "Y的坐标 " + p.getY()); System.out.println(); System.out.println( "用整数表示坐标: " ); Point<Integer> p2 = new Point<Integer>( 12 , 23 ); System.out.println( "X的坐标 " + p2.getX()); System.out.println( "Y的坐标 " + p2.getY()); System.out.println(); System.out.println( "用字符串表示坐标: " ); Point<String> p3 = new Point<String>( "北纬29度" , "东经113度" ); System.out.println( "X的坐标 " + p3.getX()); System.out.println( "Y的坐标 " + p3.getY()); } } |
使用泛型过后,可减少安全隐患的存在
如果此时我们刻意传入不一样的数据类型:
Point<Double> p = new Point<Double>("北纬29度",12.22);
那么,在编译时就会报错
虽然定义了泛型,但如果你在构造函数中并未使用泛型机制的话,那么它便会把数据当作Object处理
这样做的目的主要是为了兼容JDK1.4以前的老代码,如
Point p = new Point(22.11,23.21);
最终运行结果是一样的,但在编译时却会提示警告信息
实例3:泛型方法
由上面的例子可以看到,一旦在构造方法中明确对象类型,那么整个类中就将使用同一种类型
最典型的例子是运用在集合框架里面,如:ArrayList<Integer> al = new ArrayList<Integer>();
此时,al中操作的所有对象类型便都是Integer了
可是,有时候我们并不希望固定死操作的对象,而是希望更够更加灵活的使用泛型技术
这个时候就可以尝试泛型方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | //类名后面不再定义泛型 class Print { //在方法中定义泛型 public <T> void print(T t) { System.out.println(t); } public <E> void show(E e) { System.out.println(e); } } public class Demo { public static void main(String[] args) { Print p = new Print(); p.print( 12 ); p.print( "hello" ); p.show( new Integer( 33 )); p.show( 23 ); } } |
其实这样一来,与在方法中使用Object对象已经没有什么太大区别了
何况,JDK1.5之后加入了自动拆装箱功能,省去了需要向下转型的麻烦
实例4:泛型接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //定义一个泛型接口 interface Inter<T> { public void print(T t); } //实现方式一: class InterDemo1 implements Inter<String> { public void print(String t) { System.out.println( "print: " + t); } } //实现方式二: class InterDemo2<T> implements Inter<T> { public void print(T t) { System.out.println( "print: " + t); } } class Demo { public static void main(String[] args) { InterDemo1 id1 = new InterDemo1(); id1.print( "hello" ); InterDemo2<Integer> id2 = new InterDemo2<Integer>(); id2.print( new Integer( 23 )); } } |
实现泛型接口的方式有两种,一种是在实现的时候指定泛型类型
另一种是依然使用泛型,在构造的时候确定泛型类型
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步