如何返回范型类型呢?


public class Test {
    public static void main(String[] args) {
        Class<?> aliyun = ResType.of("aliyun").getType();
        try {
            Object obj = aliyun.newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
class TencentData {
    private String id;
}

class AliyunData {
    private String id;
}

class Response<T> {
    private T date;

    public T getDate() {
        return date;
    }

    public void setDate(T date) {
        this.date = date;
    }
}

interface Convert {
    Class<?> getType();
}

enum ResType implements Convert {
    ALIYUN("aliyun") {
        @Override
        public Class<?> getType() {
            return Response<AliyunData>.class; //这句报错 ,怎么返回?
        }
    },
    TENCENT("tencent") {
        @Override
        public Class<?> getType() {
            return Response<TencentData>.class; //这句报错,怎么返回?
        }
    };
    private final String depict;

    ResType(String depict) {
        this.depict = depict;
    }

    public String getDepict() {
        return depict;
    }

    public static ResType of(String depict) {
        for (ResType temp : ResType.values()) {
            if (temp.getDepict().equals(depict)) {
                return temp;
            }
        }
        return null;
    }
}

我需要返回Response的具体类型带着范型的,我该如何操作?
我需要的是类型,不需要实例。
这个只是demo,实际业务比这个复杂点,还有我不想绕过这个知识点从其它途径完成功能。

public static ResponseDTO of(T data) {
ResponseDTO responseDTO = new ResponseDTO<>();
responseDTO.setData(data);
return responseDTO;
}

public static Response getType() {
return new Response();
}

不用你返回啊,通过你返回的对象ResponseDTO,获得date属性的一个对象,在通过该对象.getClass()就能返回该对象类型

不知道这样符不符合你的需求,如果可以的话望采纳

    public class Test {
    
        public static void main(String[] args) {
            Class<?> aliyun = ResType.of("aliyun").aClass;
            try {
                Object obj = aliyun.newInstance();
    
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    class TencentData {
        private String id;
    }
    
    class AliyunData {
        private String id;
    }

    class Response<T> {
        private T date;
    
        public T getDate() {
            return date;
        }
    
        public void setDate(T date) {
            this.date = date;
        }
    }

    enum ResType {
        ALIYUN("aliyun", AliyunData.class),
        TENCENT("tencent",TencentData.class),
        ;

        public final String depict;
        
        public final Class<?> aClass;

        ResType(String depict, Class<?> aClass) {
            this.depict = depict;
            this.aClass = aClass;
        }

        public static ResType of(String depict) {
            for (ResType temp : ResType.values()) {
                if (temp.depict.equals(depict)) {
                    return temp;
                }
            }
            return null;
        }
    }

我自己找到答案了 ,使用Google,com.google.gson.reflect.TypeToken类解决
new TypeToken<HuobiRes>(){}.getRawType();