zoukankan      html  css  js  c++  java
  • 泛型

    一,Demo1:

    public class Point<T> {
        public T x;
        public T 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 GenDemo {
        public static void main(String args[])
        {
            Point<Float> p=new Point<Float>();
            p.setX(11.9f);
            p.setY(20.8f);
            System.out.println(p.getX());
            System.out.println(p.getY());
        }
    }

    二,Demo2泛型擦除

    public class Point<T> {
        public T x;
        public T 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;
        }
        //构造方法使用泛型
        Point(T x,T y)
        {
            this.setX(x);
            this.setY(y);
        }
    }
    public class GenDemo {
        public static void main(String args[])
        {
            Point<Float> p=new Point<Float>(11.9f,20.8f);
            System.out.println(p.getX());
            System.out.println(p.getY());
       
            //泛型擦除后按照object类型操作
            Point p1=new Point(11.2f,11.3f);
            System.out.println(p1.getX());
            System.out.println(p1.getY());
        }
    }

    三,泛型通配符

    泛型里面不能够自动转型

    public class GenDemo {
        public static void main(String args[])
        {
            Point<Float> p=new Point<Float>(11.9f,20.8f);
            System.out.println(p.getX());
            System.out.println(p.getY());
       
            //泛型擦除后按照object类型操作
            Point p1=new Point(11.2f,11.3f);
            System.out.println(p1.getX());
            System.out.println(p1.getY());
           
            //泛型通配符接收
            Point<Object> p3=new Point<Object>();
            Point<Integer> p4=new Point<Integer>();
            fun(p3);
            fun(p4);       
        }
        //泛型通配符接收不同的限定类型
        public static void  fun(Point<?> po)
        {
            System.out.println(po.getX());
            System.out.println(po.getY());
        }
    }

    四,泛型上限

    //设置泛型上限,那么T的数据类型不能超过Number
    public class Point<T extends Number> {
        public T x;
        public T 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;
        }
        Point(){}
        //构造方法使用泛型
        Point(T x,T y)
        {
            this.setX(x);
            this.setY(y);
        }
    }

    public class GenDemo {
        public static void main(String args[])
        {
            Point<Float> p=new Point<Float>(11.9f,20.8f);
            System.out.println(p.getX());
            System.out.println(p.getY());
       
            //泛型擦除后按照object类型操作
            Point p1=new Point(11.2f,11.3f);
            System.out.println(p1.getX());
            System.out.println(p1.getY());
           
            //泛型通配符接收
            Point<Object> p3=new Point<Object>(1,2);
            Point<Integer> p4=new Point<Integer>(6,9);
            fun(p3);
            fun(p4);   
           
            //最大操作父类为Number
            Point<Integer> p6=new Point<Integer>();
        }
        //泛型通配符接收不同的限定类型
        public static void  fun(Point<? extends Number> po)
        {
            System.out.println(po.getX());
            System.out.println(po.getY());
        }
    }

    五,泛型下限

    //设置泛型上限,那么T的数据类型不能超过Number
    public class Point<T> {
        public T x;
        public T 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;
        }
        Point(){}
        //构造方法使用泛型
        Point(T x,T y)
        {
            this.setX(x);
            this.setY(y);
        }
    }

    public class GenDemo {
        public static void main(String args[])
        {
            Point<Float> p=new Point<Float>(11.9f,20.8f);
            System.out.println(p.getX());
            System.out.println(p.getY());
       
            //泛型擦除后按照object类型操作
            Point p1=new Point(11.2f,11.3f);
            System.out.println(p1.getX());
            System.out.println(p1.getY());
           
            //泛型通配符接收
            Point<Object> p3=new Point<Object>(1,2);
            Point<Integer> p4=new Point<Integer>(6,9);
            fun(p3);   
           
            //最大操作父类为Number
            Point<Integer> p6=new Point<Integer>();
        }
        //泛型通配符接收不同的限定类型
        //在泛型方法中设定泛型的下限
        public static void  fun(Point<? super String> po)
        {
            System.out.println(po.getX());
            System.out.println(po.getY());
        }
    }

  • 相关阅读:
    关于findViewById返回空指针的错误
    android客户端向服务器发送图片和文字,类似于发微博。能用json格式发送吗?
    nodejs 学习资料大全
    篇章三:[AngularJS] 使用AngularCSS動態載入CSS
    篇章二:[AngularJS] 使用AngularAMD動態載入Service
    篇章一:[AngularJS] 使用AngularAMD動態載入Controller
    Angular 资料大集合
    js-音乐播放器,播放|暂停|滑块的功能
    JS-以鼠标位置为中心的滑轮放大功能demo1
    使用 Electron 构建桌面应用(拖动控制篇)
  • 原文地址:https://www.cnblogs.com/jinzhengquan/p/1941939.html
Copyright © 2011-2022 走看看