day05泛型类和泛型方法

一、泛型类

public class MapList<K,V> {
    
    public  static <K,V> MapList<K,V> of(){
        return  new MapList<>();
    }
    
}

二、泛型方法

package com.OnClass.day05.reflect;

public class GenericDemo {
    public static void main(String[] args) {
        GenericDemo demo=new GenericDemo();
        //如果返回值的类型是String,并对x,y,z类型进行定义,其中test方法前面的<>可以删掉
        String s1=demo.<String,String,String>test("","");
        //也可以定义返回类型是Integer
        Integer s2=demo.<String,String,Integer>test("","");

    }

    //泛型方法定义语法如下,当x,y,z的类型还不确定时候,使用泛型方法
    public <X,Y,Z> Z test(X x,Y y){
        return null;
    }
}
原文地址:https://www.cnblogs.com/zzzao/p/11135326.html