Java泛型

泛型的理解和好处:

1.不能对加入到集合ArrayList中的数据类型进行约束(不安全)

2.遍历的时候,需要进行类型转换,如果集合中的数据量较大,对效率有影响

3.在类型声明或实例化时只要指定好需要的具体类型即可

4.泛型的作用是:可以在类型声明是通过一个标识类型中某个属性的类型,或者是某个方法的返回值的类型,或者是差数类型。

复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        Nerson<String> person = new Nerson<String>("100");
        System.out.println(person.getS());
    }
}
class Nerson<E>{
    E s;
    public Nerson(E s) {
        this.s = s;
    }

    public E getS() {
        return s;
    }

    public void setS(E s) {
        this.s = s;
    }
}
复制代码

 

 

 

复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        //再给泛型指定具体类型后,可以传入该类型或者其子类类型
        Pig<A> aPig = new Pig<A>(new A());
        //因为我给定的泛型是A这里却用了B类的对象
        //Pig<A> aPig1 = new Pig<A>(new B());
        Pig<A> pig = new Pig<>(new B());
    }
}

class A {};

class B extends A{};

class Pig<E> {
    E e;

    public Pig(E e) {
        this.e = e;
    }
}
复制代码
自定义泛型
普通泛型的数组,不能初始化
静态方法中不能使用类型泛型
泛型类的类型,实在创建对象是确定的(因为创建对象时,需要指定确定类型)
如果在创建对象时,没有指定类型,默认为Object
/*
基本语法:
class 类名<T,R....>{//...表示可以有多个泛型
成员
}
*/
复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
    
    }
}

class Tiger<T, R, M>{
    String name;
    R r;
    M m;
    T t;


    public Tiger(String name, R r, M m, T t) {
        this.name = name;
        this.r = r;
        this.m = m;
        this.t = t;
        //因为数组在new不能确定T的类型,就无法在内存开空间
        T[] ts = new T[8];
    }

    //静态方法中不能使用类型泛型
    public static void m1(M m){}

    public String getName() {
        return name;
    }

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

    public R getR() {
        return r;
    }

    public void setR(R r) {
        this.r = r;
    }

    public M getM() {
        return m;
    }

    public void setM(M m) {
        this.m = m;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }

    @Override
    public String toString() {
        return "Tiger{" +
                "name='" + name + '\'' +
                ", r=" + r +
                ", m=" + m +
                ", t=" + t +
                '}';
    }
}
复制代码
自定义泛型接口
在接口中静态成员不能使用泛型(这个和泛型类规定一样)
泛型接口的类型,在继承接口或者实现接口时确定
没有指定类型,默认为Object
基本语法
interface 接口名<T,R..>{
}
复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
     
    }
}
//泛型接口的类型,在继承接口或者实现接口时确定
class aa implements IUsb<String,String>{


    @Override
    public String get(String i) {
        return null;
    }

    @Override
    public void hi(String s) {

    }

    @Override
    public void run(String r1, String r2, String u1, String u2) {

    }
}
//当我们去实现IA接口时,因为IA在继承IUsb接口,指定U为String R为Double
//在实现IUsb接口方法时,使用Sting替代U,使用Double替换R

class AA implements IA {

    @Override
    public Double get(String s) {
        return null;
    }

    @Override
    public void hi(Double aDouble) {

    }

    @Override
    public void run(Double r1, Double r2, String u1, String u2) {

    }
}

interface IUsb<U,R>{
    int n = 10;

    //普通方法中,可以这样使用
    R get(U u);

    void hi(R r);

    void run(R r1, R r2, U u1,U u2);
    //在jdk8中,可以在接口中使用默认方法,也可容易使用泛型
    default R method(U u){
        U name;
        return null;
    }
}

//让IA接口去继承TUsb接口
interface IA extends IUsb<String,Double>{

}
复制代码
1.泛型方法,可以定义在普通类中,也可以定义在泛型类中
2.当泛型方法被调用时,类型会确定
3.public void eat(E e){},修饰符后没有<T,R...>eat方法不是泛型方法,而是使用了泛型
复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
         /*
            1.泛型方法,可以定义在普通类中,也可以定义在泛型类中
            2.当泛型方法被调用时,类型会确定
            3.public void eat(E e){},修饰符后没有<T,R...>eat方法不是泛型方法,而是使用了泛型
         */
    }
}

class Car {
    public void rund(){//这是一个普通方法

    }
    public <T,R> void fly(T t,R r){//泛型方法

    }
}

class Fish<T,R>{//泛型类
    public void run(){//普通方法

    }

    public<U,P> Fish(U u,P p){//在泛型类中定义泛型方法

    }
    public void gg(T t,R r){//不是泛型方法,只是泛使用

    }
}
复制代码

1.泛型不具备继承性List<Object> list = new ArrayList<String>();

2.<?>:支持任意泛型类型

3.<? extends  A>:支持A类以及A类的之类,规定了泛型的上限

4.<? super A>:支持A类以及A类的f父类,不限于直接父类,规定了泛型的下限

泛型练习:

 

复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {

    }
    @Test
    public void testList(){
        DAO<User> dao = new DAO<>();
        dao.save("001",new User(1,10,"jack"));
        dao.save("002",new User(2,18,"mqs"));
        dao.save("003",new User(3,23,"klo"));
        dao.delete("001");
        System.out.println(dao.get("001"));
        List<User> list = dao.list();
        System.out.println(list);
    }
}

 class User{
    private int id;
    private int age;
    private String name;

    public User(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

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


class DAO<T>{
    private Map<String,T> map= new HashMap<>();
    public void save(String id, T entity){
        map.put(id,entity);
    }

    public T get(String id){
        map.get(id);
        return map.get(id);
    }

    public void update(String id, T entity){
        map.put(id,entity);
    }

    public List<T> list(){
        List<T> List = new ArrayList<>();
        Set keyset = map.keySet();
        for (Object key:keyset){
            List.add(map.get(key));
        }
        return List;
    }

    public void delete(String id){
        map.remove(id);
        System.out.println(map.remove(id));
    }

}
复制代码

泛型练习:

复制代码
public class Application {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        employees.add(new Employee("make", 18000.0, new MyDate(2000,1,20)));
        employees.add(new Employee("make", 20000.0, new MyDate(2001,3,2)));
        System.out.println(employees);

        employees.sort(new Comparator<Employee>() {
            @Override
            public int compare(Employee emp1, Employee emp2) {
                if (!(emp1 instanceof Employee && emp2 instanceof Employee)){
                    System.out.println("类型不配备。。");
                    return 0;
                }
                int i = emp1.getName().compareTo((emp2.getName()));
                if (i!=0){
                    return i;
                }
                //如果name相同,就比较birthday-year
                return emp1.getBirthday().compareTo(emp2.getBirthday());
            }
        });
        System.out.println(employees);
    }
}

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 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 String toString() {
        return "MyDate{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                '}';
    }

    @Override
    public int compareTo(MyDate o) {
        //如果name相同,就比较birthday-year
        int year = o.getYear()-o.getYear();
        if (year != 0){
            return year;
        }
        int month = o.getMonth() - o.getMonth();
        if (month!=0){
            return month;
        }
        int day = o.getDay() - o.getDay();
        return day;
    }
}

class Employee{
    private String name;
    private Double sal;
    MyDate birthday;

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

    public String getName() {
        return name;
    }

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

    public Double getSal() {
        return sal;
    }

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

    public MyDate getBirthday() {
        return birthday;
    }

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

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

 

posted @   贝勒爷ma  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示