Loading

获取Class对象

package reflect;

import pass.User;

/**
 * @Author: DengJia
 * @Date: 2021/12/1 9:15
 * @Description: 获取Class对象的四种方式
 */

public class MainTest {
    public static void main(String[] args) throws ClassNotFoundException {
        User u = new User();

        // 1.
        Class<User> uC1 = User.class;
        // 2.
        Class<?> uC2 = Class.forName("top.dj.User");
        // 3.
        Class<? extends User> uC3 = u.getClass();
        // 4.
        Class<?> uC4 = ClassLoader.getSystemClassLoader().loadClass("top.dj.User");
    }
}
posted @ 2021-12-01 09:30  溫柔の風  阅读(33)  评论(0编辑  收藏  举报