泛型的学习

在集合中使用泛型

package com.www.java1;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class MyDate implements Comparable<MyDate>{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    @Override
    public String toString() {
        return year + "-" + month + "-" + day;
    }

    @Override
    public int compareTo(MyDate o) {
        int minYear = this.getYear() - o.getYear();
        if(minYear != 0){
            return minYear;
        }
        int minMonth = this.getMonth() - o.getMonth();
        if(minMonth != 0){
            return minMonth;
        }
        return this.getDay() - o.getDay();

    }

}

package com.www.java1;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class Employee implements Comparable<Employee>{
    private String name;
    private int age;
    private MyDate birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    public Employee(String name, int age, MyDate birthday) {
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    //name排序

    @Override
    public int compareTo(Employee o) {
        return this.name.compareTo(o.name);
    }
}

package com.www.java1;

import org.junit.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class TreeSetTest {
    @Test
    public void test(){
        MyDate myDate = new MyDate(1995, 6, 30);
        MyDate myDate1 = new MyDate(1995, 10, 30);
        MyDate myDate2 = new MyDate(1996, 8, 9);
        MyDate myDate3 = new MyDate(1994, 1, 25);
        MyDate myDate4 = new MyDate(1993, 3, 7);
        Employee employee = new Employee("张三", 27, myDate);
        Employee employee1 = new Employee("李四", 27, myDate1);
        Employee employee2 = new Employee("王五", 26, myDate2);
        Employee employee3 = new Employee("赵六", 28, myDate3);
        Employee employee4 = new Employee("钱三", 29, myDate4);

        TreeSet<Employee> set = new TreeSet<Employee>();
        set.add(employee);
        set.add(employee1);
        set.add(employee2);
        set.add(employee3);
        set.add(employee4);

        Iterator<Employee> iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        Comparator com = new Comparator<Employee>(){
            @Override
            public int compare(Employee o1, Employee o2) {
                MyDate date1 = o1.getBirthday();
                MyDate date2 = o2.getBirthday();
                //方式一
//
//                int minYear = date1.getYear() - date2.getYear();
//                if(minYear != 0){
//                    return minYear;
//                }
//                int minMonth = date1.getMonth() - date2.getMonth();
//                if(minMonth != 0){
//                    return minMonth;
//                }
//                return date1.getDay() - date2.getDay();
                //方式二
                return date1.compareTo(date2);
            }
        };
        TreeSet<Employee> set1 = new TreeSet(com);
        set1.add(employee);
        set1.add(employee1);
        set1.add(employee2);
        set1.add(employee3);
        set1.add(employee4);

        System.out.println("**********");

        Iterator<Employee> iterator1 = set1.iterator();
        while(iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
    }
}

package com.www.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * 一、集合中使用泛型
 * 1.集合接口或集合类在jdk5.0时都修改为代泛型的结构
 * 2.在实例化集合类时,可以指明具体的泛型类型(没指明时,默认object)
 * 3.之后凡是使用到类的泛型位置,都要使用指明的类型
 * 二、如何自定义泛型结构(接口、类、方法)
 *
 *
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class GenericTest {
    //在集合中使用泛型的情况
    @Test
    public void test(){
        ArrayList list = new ArrayList();
        //需要存入学生成绩
        list.add(23);
        list.add(83);
        list.add(12);
        list.add(95);
        list.add(100);
        list.add(72);
        //问题一:类型不安全
        list.add("Tom");

        for(Object obj:list){//这里是Object
            Integer i = (Integer) obj;//问题二:java.lang.ClassCastException
            System.out.println(i);
        }


    }
    //在集合中使用泛型
    @Test
    public void test1(){
        ArrayList<Integer> list = new ArrayList<Integer>();//类型不能是基本数据类型,可以是包装类
        list.add(12);
        list.add(42);
        list.add(63);
        list.add(72);
        //编译时就会检查
//        list.add("Tom");//public boolean add(E e) 凡是方法中也使用泛型的地方,都得是Integer

        //方式一
        for(Integer score : list){//这里是Integer
            System.out.println(score);//避免强转操作
        }

        //方式二
        Iterator<Integer> iterator = list.iterator();//能有<Integer>前提是方法中定义过
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }}

自定义泛型类

package com.www.java;

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

/**
 * 自定义泛型类
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class Order<T> {
    String name;
    int age;

    //类的内部结构可以使用泛型
    T orderT;

    public Order() {//构造器这里没有<T>
        //编译不通过
//        T[] arr = new T[10];
        T[] arr = (T[])new Object[10];//T类型数组,这里的T是泛型
    }

    public Order(String name, int age, T orderT) {
        this.name = name;
        this.age = age;
        this.orderT = orderT;
    }

    public T getOrderT() {
        return orderT;
    }

    public void setOrderT(T orderT) {
        this.orderT = orderT;
    }

    @Override
    public String toString() {
        return "Order{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", orderT=" + orderT +
                '}';
    }
    //泛型方法:出现了新的泛型,与类的泛型参数无关
    public<E> List<E> copyFronArrayToList(E[] arr){

        ArrayList<E> arrayList = new ArrayList<>();
        for(E e:arr){
            arrayList.add(e);
        }
        return arrayList;
    }
    //如果写成public List<E> copyFronArrayToList(E[] e){
    //
    //    }会认为E是一个自定义的类,而不是泛型
}

package com.www.java;

/**
 *
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
//情况一:父类是泛型类,且指明了泛型类型,子类则为普通类
public class SubOrder extends Order<Integer>{

}


package com.www.java;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
//情况二:子类依然是泛型类
public class SubOrder1<T> extends Order<T>{

}

package com.www.java;

import org.junit.Test;

import java.util.List;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class GenericTest1 {
    @Test
    public void test(){
        //不指明类型,默认object
        //如果定义了,建议使用时指明类型
        Order order1 = new Order();
        order1.setOrderT(123);
        order1.setOrderT("哈哈");
        //指明类型
        Order<String> order = new Order<>("Tom",1100,"心情不好");
        order.setOrderT("心情不错");
        System.out.println(order);

    }
    @Test
    public void test2(){
        SubOrder subOrder = new SubOrder();
        SubOrder1<String> subOrder1 = new SubOrder1<>();
    }
    @Test
    public void test3(){
        Order<String> order = new Order<>();
        Integer[] arr = new Integer[]{1,2,3,4};
        String[] arr1 = new String[]{"Tom"};
        List<Integer> list = order.copyFronArrayToList(arr);
        System.out.println(list);
        //错误
//        List<Integer> list1 = order.copyFronArrayToList(arr1);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
共性–特性–操作目标

package com.www.java2;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */

public class Customer {
}

package com.www.java2;

import java.util.List;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
//datd access object
public class DAO <E>{//共性操作

    //添加一条记录
    public void add(E e){

    }

    //删除一条记录
    public boolean delet(int index){
        return false;
    }

    //修改一条记录
    public void update(int index,E e){

    }
    //查询一条记录
    public E getIndex(int index){
        return null;
    }

    //查询多条记录
    public List<E> getForList(int index){
        return null;
    }
}


package com.www.java2;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
//针对customer的操作
public class CustomerDAO extends DAO <Customer> {
}

package com.www.java3;

import org.junit.Test;

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

/**
 * 1.泛型在继承方面的体现
 *
 * 2.通配符
 *
 * 3.有限制条件的通配符
 *
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class GerericTest {
    /*
    1.泛型在继承方面的体现
     */
    @Test
    public void test(){
        Object obj = null;
        String str = null;
        obj = str;

        Object[] arr1 = null;
        String[] arr2 = null;
        arr1 = arr2;

        List<Object> list1 = null;
        List<String> list2 = null;
        //list1、list2不具有子父类关系,乃是并列
//        list1 = list2;//错误

    }
    /*
    2.通配符(?)的使用
     */
    //类A和类B是子父类关系,G(A)、G(B)是并列关系,二者共同父类G(?)
    @Test
    public void test1(){

        List<Object> list1 = null;
        List<String> list2 = null;

        List<?> list3 = null;
        //二者共同父类G(?)
        list3 = list1;
        list3 = list2;

        show(list1);
        show(list2);

        //添加:具有通配符的类不能添加数据,除了添加null
        list2.add("Aa");
        list2.add("BB");

//        list3.add("CC");//编译报错

        //获取:
        Object o = list3.get(0);
        System.out.println(o);
    }
    public void show(List<?> list){
        Iterator<?> iterator = list.iterator();
        while(iterator.hasNext()){
            Object obj = iterator.next();
            System.out.println(obj);
        }
    }
    /*
    3.有限制条件的通配符
      ? extends Person:Person及子类可以赋值
      ? super Person:Person及父类可以赋值
     */
    public void test2(){

        List<? extends Person> list1 = null;//<=
        List<? super Person> list2 = null;//>=

        List<Students> list3 = new ArrayList<>();
        List<Person> list4 = new ArrayList<>();
        List<Object> list5 = new ArrayList<>();

        //Person及子类可以赋值
        list1 = list3;
        list1 = list4;
//        list1 = list5;

        //Person及父类可以赋值
//        list2 = list3;
        list2 = list4;
        list2 = list5;

        //读取
        list1 = list3;//3是students
        Person person = list1.get(0);
//        Students person = list1.get(0);//错误

        list1 = list4;//4是person
        Person person1 = list1.get(0);
        Object person2 = list1.get(0);

        list2 = list4;
        Object object = list2.get(0);
//        Person person3 = list2.get(0);//错误

        //写入
//        list1.add(new Person());//错误

        list2.add(new Person());
        list2.add(new Students());

    }
}

package com.www.java3;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class Person {
}

package com.www.java3;

/**
 * @author www
 * @creat 2022-{MONTH}-{DAY}
 */
public class Students extends Person{
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

新手学java2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值