zoukankan      html  css  js  c++  java
  • java反射的四个加载器

    加载器是个好东西

    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
            // 反射String类的所有构造方法
            Class clz = Student.class;
    
            System.out.println("所有构造方法");
            // Constructor[] cons = clz.getConstructors();
            Constructor[] cons = clz.getDeclaredConstructors();
            for (Constructor con : cons) {
                // System.out.println("访问修饰权限:" +
                // Modifier.toString(con.getModifiers()));
                // System.out.println("方法名:" + con.getName());
                // System.out.println("****************************");
                System.out.println(Modifier.toString(con.getModifiers()) + " "
                        + con.getName());
            }
    
            
            //找无参的构造方法    Student s = new Student();
            Constructor con = clz.getDeclaredConstructor();
            Object obj = con.newInstance();
            System.out.println(obj);
            
            //找带string,int类型参数的构造方法 Student s = new Student("zhangsan",12)
            con = clz.getDeclaredConstructor(String.class, int.class);
            obj = con.newInstance("zhangsan", 12);
            System.out.println(obj);
            
            con = clz.getDeclaredConstructor(String.class);
            
            }
  • 相关阅读:
    毕业设计
    毕业设计
    毕业设计
    毕业设计
    layui table
    毕业设计
    Echart图标统计
    Pxe自动化安装
    Linux运维常用脚本整理
    Zabbix 一键部署
  • 原文地址:https://www.cnblogs.com/paoge/p/13542885.html
Copyright © 2011-2022 走看看