JAVA中的泛型

1.为什么需要泛型

  • java属于强类型编程语言,变量在使用之前,需要先进行定义,而地定义个变量是必须要指定其数据类型;
    例如:声明一个类型为Object类型,然后向下转型就会报错;
public static void main(String[] args) throws Exception {

        Circle circle = new Circle();
        circle.setX(12);
        circle.setY(9);
        System.out.println(circle);

        Circle circle2 = new Circle();
        circle2.setX("圆心横坐标x");
        circle2.setY("圆心纵坐标y");
        System.out.println(circle2);

        //Object转Double类型报错
        Double x1 = (Double)circle2.getX();
        Double y1 = (Double)circle2.getY();

        System.out.println(circle2);
    }


    public static class Circle{

        private Object x;
        private Object y;
        /**
         * 获取x
         * @return the x
         */
        public Object getX() {
            return x;
        }
        /**
         * 设置x
         * @param x the x to set
         */
        public void setX(Object x) {
            this.x = x;
        }
        /**
         * 获取y
         * @return the y
         */
        public Object getY() {
            return y;
        }
        /**
         * 设置y
         * @param y the y to set
         */
        public void setY(Object y) {
            this.y = y;
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "Circle{"+"x="+x+", y="+y+"}";
        }
    }

运行到向下转型就会报出错误

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
    at main.main(main.java:21)
Circle{x=12, y=9}
Circle{x=圆心横坐标x, y=圆心纵坐标y}

2.泛型使用

  • 泛型类型的定义方法为:类名后面尖括号<>

例如:定义类然后在使用时指定类型

public static void main(String[] args) throws Exception {

//      Circle circle = new Circle();
//      circle.setX(12);
//      circle.setY(9);
//      System.out.println(circle);

        Circle<String,String> circle2 = new Circle<String,String>();
        circle2.setX("圆心横坐标x");
        circle2.setY("圆心纵坐标y");
        System.out.println(circle2);

        String x1 = (String)circle2.getX();
        String y1 = (String)circle2.getY();

        System.out.println(circle2);
    }


    public static class Circle<T1,T2>{

        private T1 x;
        private T2 y;
        /**
         * 获取x
         * @return the x
         */
        public T1 getX() {
            return x;
        }
        /**
         * 设置x
         * @param x the x to set
         */
        public void setX(T1 x) {
            this.x = x;
        }
        /**
         * 获取y
         * @return the y
         */
        public T2 getY() {
            return y;
        }
        /**
         * 设置y
         * @param y the y to set
         */
        public void setY(T2 y) {
            this.y = y;
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "Circle{"+"x="+x+", y="+y+"}";
        }
    }

3.限制泛型类型的使用类型

  • 我们需要对类型参数做一定的限制,只能传递部分参数类型传递其他类型则会报错,例如我们在前面的类型限制只能传递数字类型
    例如:
public static void main(String[] args) throws Exception {

        Circle<Integer,Integer> circle2 = new Circle<Integer,Integer>();
        circle2.setX(12);
        circle2.setY(13);
        System.out.println(circle2);
    }

    public static class Circle<T1 extends Number,T2 extends Number>{

        private T1 x;
        private T2 y;
        /**
         * 获取x
         * @return the x
         */
        public T1 getX() {
            return x;
        }
        /**
         * 设置x
         * @param x the x to set
         */
        public void setX(T1 x) {
            this.x = x;
        }
        /**
         * 获取y
         * @return the y
         */
        public T2 getY() {
            return y;
        }
        /**
         * 设置y
         * @param y the y to set
         */
        public void setY(T2 y) {
            this.y = y;
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "Circle{"+"x="+x+", y="+y+"}";
        }
    }

4.类型擦除

我们在使用泛型的过程中,如果没有指定数据类型,那么将会擦除泛型类型:

Circle circle2 = new Circle();
circle2.setX("圆心横坐标x");
circle2.setY("圆心纵坐标y");
System.out.println(circle2);

String x1 = (String)circle2.getX();
String y1 = (String)circle2.getY();

System.out.println(circle2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值