zoukankan      html  css  js  c++  java
  • 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);
            }    
        }

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

  • 相关阅读:
    蓝桥杯训练 | 二分和前缀和 | 02
    广西师大数据结构 | 2015年真题 | 02
    广西师大数据结构 | 2014年真题 | 01
    Centos添加开机自启项
    SUSE系统添加开机自启项
    LNMP
    源码编译安装 libiconv-1.14(php依赖)
    lnmp
    centos添加epel源
    系统挂在镜像
  • 原文地址:https://www.cnblogs.com/newbest/p/10054101.html
Copyright © 2011-2022 走看看