gpt4 book ai didi

java - 在java中获取List 的.class

转载 作者:行者123 更新时间:2023-12-02 22:43:52 26 4
gpt4 key购买 nike

我有以下功能

编辑:将 int 更改为 Long 类

protected <T> ApiResponse<T> getApiResponse(Object obj, Class<T> clasx) 

如何通过List<Long>类作为第二个参数?

我可以通过List<Long>.class但这不起作用?

最佳答案

Type erasure意味着你只能通过List.class .

来自链接文档:

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

For instance, Box<String> is translated to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime. The following operations are not possible:

public class MyClass<E> {
public static void myMethod(Object item) {
// Compiler error
if (item instanceof E) {
...
}
E item2 = new E(); // Compiler error
E[] iArray = new E[10]; // Compiler error
E obj = (E)new Object(); // Unchecked cast warning
}
}

The operations shown in bold are meaningless at runtime because the compiler removes all information about the actual type argument (represented by the type parameter E) at compile time.

关于java - 在java中获取List <int>的.class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com