java中反射的理解

记录是为了更好的成长!

 

1、java加载class的三种方式

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //加载类的三种方式
        //1、使用new关键字方法
        User user = new User();
        Method[] methods = user.getClass().getDeclaredMethods();
        for(Method method:methods) {
            System.out.println(method);
        }
        
        //2、调用 Class.forName() 方法
        Class clazz= Class.forName("com.boot.test.User");
        Object user1 = clazz.newInstance();
        Method[] methods1 = user1.getClass().getDeclaredMethods();
        System.out.println(" Class.forName()+++++++++++++++++++++++++++++++");
        for(Method method:methods1) {
            System.out.println(method);
        }
        
        //3、调用某个 ClassLoader 实例的 loadClass() 方法
        Class clazz1 = ClassLoader.getSystemClassLoader().loadClass("com.boot.test.User");
        Method[] methods2 = clazz1.getClass().getDeclaredMethods();
        System.out.println(" loadClass()+++++++++++++++++++++++++++++++");
        for(Method method:methods2) {
            System.out.println(method);
        }    
    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

以上内容代表个人观点,仅供参考,不喜勿喷。。。

 

posted @ 2018-12-02 16:56  刚刚睡醒  阅读(123)  评论(0编辑  收藏  举报