泛型的使用!

demo 01
Demo01:

public class Dome01 {
    public static void main(String[] args) {
        // 为了解决通用性的问题,jdk提供了封装好的集合,但是集合可以存放不同类型的数据
        List list = new ArrayList();
        list.add("111");
        list.add(111);
        list.add(true);
        list.add(11.1);
        System.out.println(list);
        // 加入泛型,做约束
        ArrayList alist = new ArrayList<>();
        alist.add("111");
        // alist.add(111); 加入了泛型,不可以存储泛型之外的数据类型
    }
}

demo02
Demo02:

public class Dome01 {
    public static void main(String[] args) {
        Pari pari1 = new Pari<>(1,2);
        System.out.println(pari1);

        Pari pari2 = new Pari<>(1.1,2.2);
        System.out.println(pari2);

        Pari pari3 = new Pari<>("haha","hehe");
        System.out.println(pari3);
    }
}

Pair:

public class Pari {  // type
    private  T first;
    private  T second;
    // 构造方法
    public  Pari(){
    }
    public Pari(T first, T second) {
        this.first = first;
        this.second= second;
    }
    // setter & getter
    public T getFirst() {
        return first;
    }

    public void setFirst(T first) {
        this.first = first;
    }

    public T getSecond() {
        return second;
    }

    public void setSecond(T second) {
        this.second = second;
    }

    // toString
    @Override
    public String toString() {
        return "Pair{" +
                "first=" + first +
                ", second=" + second +
                '}';
    }
}

demo03
ArrayAlg:

public class ArrayAlg {
    // 求出通用的数据中间位置的值
    public static  T getMiddle(T[] a ){
        return  a[a.length / 2];
    }

    /* public static String getMiddle(String[] a) {
        return a[a.length / 2 ];
    }*/


}

Demo03

public class Dome01 {
    public static void main(String[] args) {
      Integer [] a = {1,2,3,4,5};
      System.out.println(ArrayAlg.getMiddle(a));

      String [] str = { "111","222","333","444","555","666","777"};
      System.out.println(ArrayAlg.getMiddle(str));
    }
}

demo04
Shape:

public abstract class Shape {
    public abstract void xingzhuang();
}

Circle:

public class Circle extends Shape {
    @Override
    public void xingzhuang(){
        System.out.println("正在画圆");
    }
}

Canvas:

public class Canvas {
    // 在画布上画一个
    public void draw(Shape shape) {
        shape.xingzhuang();
    }
    // 在画布上画多个
    public void drawAll (List< ? extends Shape> shapesList) {
        for ( Shape s : shapesList) {
            s.xingzhuang();
        }
    }

}

Rectangle:

public class Rectangle extends Shape {
    @Override
    public void xingzhuang() {
        System.out.println("正在画矩形");
    }
}

Demo04:

public class Dome04 {
    public static void main(String[] args) {
        Canvas canvas = new Canvas();
        Circle circle = new Circle();
        canvas.draw(circle);

        // 定义多个形状,组成一个数组列表,传入
        Circle circle1 = new Circle();
        Rectangle rectangle = new Rectangle();
        Rectangle rectangle1 = new Rectangle();
        Circle circle2 = new Circle();
        // 准备一个集合
        ArrayList list = new ArrayList<>();
        list.add(circle1);
        list.add(rectangle);
        list.add(rectangle1);
        list.add(circle2);
        canvas.drawAll(list);

    }
}

demo05
Demo05:

public class Dome05 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        List tempList = list; //  泛型的擦除
        tempList.add(1213);
        System.out.println(tempList);
    }
}

你可能感兴趣的:(泛型的使用!)