2021-05-11

课堂代码

package edu.xcdq.demo01;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huyuansong
 * @date 2021/5/11 15:10
 */
public class Demo01 {
    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); 加入了泛型,不可以存储泛型之外的数据类型





    }
}

package edu.xcdq.demo02;

/**
 * @author huyuansong
 * @date 2021/5/11 15:21
 */
public class Demo02 {
    public static void main(String[] args) {
        Pair pair1 = new Pair<>(1, 2);
        System.out.println(pair1);

        Pair pair2 = new Pair<>(1.1, 2.2);
        System.out.println(pair2);

        Pair pair3 = new Pair<>("haha", "hehe");
        System.out.println(pair3);



    }
}
package edu.xcdq.demo02;

/**
 * @author huyuansong
 * @date 2021/5/11 15:18
 */
public class Pair  {  // type
    private T first;
    private T second ;
    // 构造方法
    public Pair() {
    }
    public Pair(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 +
                '}';
    }
}

package edu.xcdq.demo03;

/**
 * @author huyuansong
 * @date 2021/5/11 15:33
 */
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 ];
    }*/

}

package edu.xcdq.demo03;

/**
 * @author huyuansong
 * @date 2021/5/11 15:33
 */
public class Demo03 {
    public static void main(String[] args) {
        Integer [] a = {1 , 2 , 3 , 4 , 5 };
        System.out.println(ArrayAlg.getMiddle(a) );

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



    }


}

package edu.xcdq.demo04;

import java.util.List;

/**
 * @author huyuansong
 * @date 2021/5/11 16:03
 */
public class Canvas {
    // 在画布上画一个
    public void draw(Shape shape) {
        shape.xingzhuang();
    }
    // 在画布上画多个
    // List< ? extends Shape > 有限制的泛型
    public void drawAll (List< ? extends Shape > shapeList ) {
        for ( Shape s : shapeList ) {
            s.xingzhuang();
        }
    }


}
package edu.xcdq.demo04;

/**
 * @author huyuansong
 * @date 2021/5/11 16:01
 */
public class Circle extends Shape {
    @Override
    public void xingzhuang() {
        System.out.println("正在画圆");
    }
}
package edu.xcdq.demo04;

import java.util.ArrayList;

/**
 * @author huyuansong
 * @date 2021/5/11 16:06
 */
public class Demo04 {
    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);


    }
}
package edu.xcdq.demo04;

/**
 * @author huyuansong
 * @date 2021/5/11 16:02
 */
public class Rectangle extends Shape {
    @Override
    public void xingzhuang() {
        System.out.println("正在画矩形");
    }
}
package edu.xcdq.demo04;

/**
 * @author huyuansong
 * @date 2021/5/11 16:00
 */
public abstract  class Shape {
    public abstract void xingzhuang() ;
}

package edu.xcdq.demo05;

import java.util.ArrayList;
import java.util.List;

/**
 * @author huyuansong
 * @date 2021/5/11 16:26
 */
public class Demo05 {
    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        List tempList = list ; // 泛型的擦除
        tempList.add(1213);
        System.out.println(tempList);


    }
}

你可能感兴趣的:(2021-05-11)