zoukankan      html  css  js  c++  java
  • 【通过反射获取无参构造方法并使用】

    package test;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    
    /**
     * @author shusheng
     * @description 通过反射获取无参构造方法并使用
     * @Email shusheng@yiji.com
     * @date 2018/12/23 23:49
     */
    public class ReflectDemo1 {
    
        public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
            // 获取字节码文件对象
            Class c = Class.forName("test.Person");
    
            // 获取构造方法
            // public Constructor[] getConstructors():所有公共构造方法
            // public Constructor[] getDeclaredConstructors():所有构造方法
            Constructor[] cons = c.getDeclaredConstructors();
            for (Constructor con : cons) {
                System.out.println(con);
            }
    
            // 获取单个构造方法
            // public Constructor<T> getConstructor(Class<?>... parameterTypes)
            // 参数表示的是:你要获取的构造方法的构造参数个数及数据类型的class字节码文件对象
            Constructor con = c.getConstructor();
    
            // Person p = new Person();
            // System.out.println(p);
            // public T newInstance(Object... initargs)
            // 使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
            Object obj = con.newInstance();
            System.out.println(obj);
    
             Person p = (Person)obj;
             p.show();
        }
    
    }
    package test;
    
    /**
     * @author shusheng
     * @description
     * @Email shusheng@yiji.com
     * @date 2018/12/23 23:49
     */
    public class Person {
        private String name;
        int age;
        public String address;
    
        public Person() {
        }
    
        private Person(String name) {
            this.name = name;
        }
    
        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public Person(String name, int age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }
    
        public void show() {
            System.out.println("show");
        }
    
        public void method(String s) {
            System.out.println("method " + s);
        }
    
        public String getString(String s, int i) {
            return s + "---" + i;
        }
    
        private void function() {
            System.out.println("function");
        }
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", address="
                    + address
                    + "]";
        }
    
    }
    终身学习者
  • 相关阅读:
    HDU3480-Division-斜率dp
    CDQ题目套路总结 [未完成]
    HDU5773-The All-purpose Zero-多校#41010-最长上升子序列问题
    POJ2763-Housewife Wind-树上单点修改区间求和
    HDU3507-Print Article-斜率dp入门题
    POJ2187-Beauty Contest-凸包
    SPOJ QTREE-Query on a tree-树链剖分-边权
    扫描系统进程和获取某进程的PID
    ConfuserEx壳
    关于Intel漏洞的学习
  • 原文地址:https://www.cnblogs.com/zuixinxian/p/11275219.html
Copyright © 2011-2022 走看看