Java reflection gets the current object

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;
}

Guess you like

Origin blog.csdn.net/weixin_42151235/article/details/130361138