public static void main(String[] args) {
User test = test(User.class);
System.out.println(User);
}
public static <T> T test(Class<T> clazz){
T target = null;
try {
// 通过反射创建对象
Constructor<T> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
target = constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
return target;
}
java反射获取当前对象
最新推荐文章于 2024-04-15 17:50:57 发布