面试题答案
一键面试以下是使用反射创建GenericClass<Integer>
实例并设置value
为10的代码:
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class ReflectionWithGeneric {
public static void main(String[] args) {
try {
// 获取GenericClass类
Class<?> genericClass = Class.forName("GenericClass");
// 获取构造函数
Constructor<?> constructor = genericClass.getConstructor(Integer.class);
// 通过构造函数创建实例
Object genericInstance = constructor.newInstance(10);
// 获取value字段
Field valueField = genericClass.getDeclaredField("value");
valueField.setAccessible(true);
// 获取并打印value的值
Object value = valueField.get(genericInstance);
System.out.println("Value: " + value);
} catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
e.printStackTrace();
}
}
}
反射过程中处理泛型的难点
- 类型擦除:Java中的泛型是在编译期进行类型检查和擦除的,运行时泛型信息会被擦除。这意味着在反射时,无法直接获取到泛型参数的具体类型,例如
GenericClass<Integer>
在运行时只知道是GenericClass
,而不知道具体的Integer
类型。
解决方法
- 显式传递类型信息:在上述代码中,由于类型擦除,反射获取构造函数时,只能通过参数类型来匹配构造函数,因此在获取构造函数时,直接使用
Integer.class
作为参数类型。这样虽然没有直接利用泛型信息,但通过显式传递参数类型,间接达到了创建GenericClass<Integer>
实例的目的。 - 使用
ParameterizedType
接口:如果需要在反射中获取泛型参数的类型,可以通过ParameterizedType
接口。例如,获取类的泛型超类的参数类型:
Type genericSuperclass = someClass.getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
for (Type type : actualTypeArguments) {
System.out.println("泛型参数类型: " + type);
}
}
在上述代码中,通过getGenericSuperclass
获取泛型超类,再通过ParameterizedType
接口获取实际的泛型参数类型。但对于简单的创建带有泛型参数的对象实例并设置值的场景,上述通过显式传递参数类型的方法已能满足需求。