4. 泛型

1. 泛型的好处

bfe3bdec1c74ea0a66afbb66c800a7e3
b7aa2c9dce412326bb2ea6f2d920dea1
72d2af7537f292948a5731cc5ec9afce

代码示例

package com.baidu.www;

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

/*
    泛型一般只和集合类相结合使用。
    泛型是JDK5的新特性,从JDK7开始,后边的泛型不用写具体的类型(菱形泛型)
 */

public class Test {
    public static void main(String[] args) {
        // 泛型
//        List<String> list1 = new ArrayList<String>();
        List<String> list1 = new ArrayList<>();

        list1.add("abc");
        list1.add("bcd");
        list1.add("cde");

        for (String s : list1) {
            System.out.println(s);
        }
    }
}

2. 泛型的说明

60973f81b3db4478054a8b9209ec0d1f

3. 泛型的语法

dd0269d56da09b43d3f69d8305fac39f

3eea6db94a3745bd30266d31ff886c9f

代码示例

package com.baidu.www;

public class Test {
    public static void main(String[] args) {
        xi<Animal> x = new xi<>(new Animal());
        x.f();
        xi<Animal> x1 = new xi<>(new Cat());
        x1.f();
    }
}

class Animal {}
class Cat extends Animal {}

class xi<T>{
    T t;

    public xi(T t) {
        this.t = t;
    }

    public void f(){
        System.out.println(t.getClass());
    }
}

练习题

7628ec286af989e9290a491fec832a58

代码示例

MyData类

package com.baidu.www;

public class MyDate implements Comparable<MyDate>{
    private int month;
    private int day;
    private int year;

    @Override
    public String toString() {
        return "MyDate{" +
                "month=" + month +
                ", day=" + day +
                ", year=" + year +
                '}';
    }

    public MyDate(int year, int month, int day) {
        this.month = month;
        this.day = day;
        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;
    }

    public int getYear() {
        return year;
    }

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

    @Override
    public int compareTo(MyDate o) {
        int yearMinus = this.year - o.year;
        if(yearMinus != 0)
            return yearMinus;
        int monthMinus = this.month - o.month;
        if(monthMinus != 0)
            return monthMinus;
        int dayMinus = this.day - o.day;
        if(dayMinus != 0)
            return dayMinus;
        return 0;
    }
}

Employee类

package com.baidu.www;

public class Employee {
    private String name;
    private int sal;
    private MyDate birthday;

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

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

    public String getName() {
        return name;
    }

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

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    public MyDate getBirthday() {
        return birthday;
    }

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

Test类

package com.baidu.www;

import java.util.ArrayList;
import java.util.Comparator;

public class Test {
    public static void main(String[] args) {
        ArrayList<Employee> arrayList = new ArrayList<>();
        arrayList.add(new Employee("shuashua", 12, new MyDate(2002, 10, 23)));
        arrayList.add(new Employee("ahuaahua", 12, new MyDate(2002, 10, 23)));
        arrayList.add(new Employee("shuashua", 12, new MyDate(2012, 10, 23)));

        System.out.println(arrayList);

        arrayList.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                if(!(o1 instanceof Employee && o2 instanceof Employee))
                    return 0;
                if(o1.getName().compareTo(o2.getName()) != 0)
                    return o1.getName().compareTo(o2.getName());

                return o1.getBirthday().compareTo(o2.getBirthday());
            }
        });

        System.out.println(arrayList);
    }
}

4. 自定义泛型类

5a9ca04cbeb34bfd14024cfd9953a998

5. 自定义泛型接口

a8a373215d8195aed923bfea95dd513d

6. 自定义泛型方法

调用方法传入参数,编译器自动确定类型。

afecd011d24e2541a914766b99134d27

7. 泛型的继承和通配符

1f68acec7c1606337c5918b966f6d2df

8. JUnit 单元测试类

779b84a04f34f931e077ddca72d6e81d

代码示例

package com.baidu.www;

import org.junit.jupiter.api.Test;

public class JUnit_ {
    public static void main(String[] args) {
        // JUnit测试
    }

    @Test
    public void m1(){
        System.out.println("1");
    }

    @Test
    public void m2(){
        System.out.println(2);
    }
}
posted @ 2025-08-25 23:19  无敌美少女战士  阅读(10)  评论(0)    收藏  举报