Reflection constructors and common methods

reflection constructor

package bull03.Constructor;
/*
 * JavaBean规范
 * 1.提供私有字段,例如:private String id;
 * 2.必须提供getter或setter方法
 * 3.提供无参构造方法
 * 4.必须实现序列化接口:
 *          java.io.Serializable
 */
public class Bean implements java.io.Serializable {
    private String name;
    private int age;

    public Bean() {
        System.out.println("调用了Bean的无参构造!");
    }

    public Bean(String name,int age) {
        System.out.println("调用了Bean的有参构造!");
        this.name = name;
        this.age = age;
    }

    private Bean(String name) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


}
package bull03.Constructor;

import java.lang.reflect.Constructor;
import org.junit.Test;

/*
 * 反射构造方法
 */
public class ConstructorDemo {
    @Test
    public void method() {
        //无参构造
        Bean be = new Bean();
    }

    @Test
    public void method1() throws Exception  {
        /*
         * 通过无参构造方法获得实例化对象
         * 1.获得Class对象clazz
         * 2.通过clazz获得无参构造
         * 3.通过构造对象获得实例化对象
         */
        //获得Class对象clazz
        Class clazz = Class.forName("bull03.Constructor.Bean");

        //获得无参构造,相当于Bena();
        //getConstructor();获得指定形参列表的构造方法
        Constructor cons = clazz.getConstructor();

        //通过构造获得实例化对象,相当于new Bean();
        //newInstance();创建实例对象,需要设置实际参数
        Object obj = cons.newInstance();
        System.out.println(obj);
    }

    @Test
    public void method2() throws Exception{
        /*
         * 通过有参构造获得实例   
         */
        //获得Class对象
        Class clazz = Class.forName("bull03.Constructor.Bean");
        //获得构造,必须指定形参.(String.class,int.class)参考获取Java对象方式2。
        Constructor cons = clazz.getConstructor(String.class,int.class);
        //获得实例对象,必须指定实际参数
        Object obj = cons.newInstance("aaa",10);
        System.out.println(obj);
    }

    @Test
    public void method3() throws Exception {
        /*
         * 无参构造快速获得实例方式
         */
        //获得Class
        Class clazz = Class.forName("bull03.Constructor.Bean");
        //通过clazz直接创建对象
        Object obj = clazz.newInstance();
        System.out.println(obj);
    }
}
  • private constructor
package bull03.Constructor;

import java.lang.reflect.Constructor;

import org.junit.Test;

/*
 * 通过私有构造创建实例化对象
 * 1.获得Class对象
 * 2.获得指定形参 私有构造
 * 3.通知JVM,运行实例化私有构造(默认不允许)
 * 4.创建实例,并设置实例参数   
 */
public class PrivateConstructor {
    @Test
    public void method() throws Exception {
        //获得Class对象
        Class clazz = Class.forName("bull03.Constructor.Bean");
        //获得指定形参 私有构造
        //clazz.getConstructor(...);获得指定对象的指定的public构造方法
        //clazz.getDeclaredConstructor(...);获得指定对象的指定的任意构造方法
        Constructor cons = clazz.getDeclaredConstructor(String.class); 
        //通知JVM,运行实例化私有构造(默认不允许)
        cons.setAccessible(true);
        //创建实例,并设置实例参数  
        Object obj = cons.newInstance("aaa");
        System.out.println(obj);
    }
}

Reflect normal method

package bull04.Method;

import java.util.Arrays;

/*
 * JavaBean规范
 * 1.提供私有字段,例如:private String id;
 * 2.必须提供getter或setter方法
 * 3.提供无参构造方法
 * 4.必须实现序列化接口:
 *          java.io.Serializable
 */
public class Bean implements java.io.Serializable {
    private String name;
    private int age;
    public String sex;

    public Bean() {
        System.out.println("调用了Bean的无参构造!");
    }

    public Bean(String name,int age) {
        System.out.println("调用了Bean的有参构造!");
        this.name = name;
        this.age = age;
    }

    private Bean(String name) {
        this.name = name;
        this.age = age;
    }

    public static void main(String[] args) {
        //将指定的数组转换成字符串
        System.out.println(Arrays.toString(args));
    }

    private void show(int num) {
        System.out.println("私有方法:"+num);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "Bean [name=" + name + ", age=" + age + "]";
    }


}
  • public type method
package bull04.Method;

import java.lang.reflect.Method;

import org.junit.Test;

/*
 * public类型方法
 */
public class PublicMethod {
    @Test
    public void method1() {
        Bean be = new Bean();
        be.setName("aaa");
        be.setAge(10);
        String name = be.getName();
        int age = be.getAge();
        System.out.println(name+"---"+age);
    }

    //用反射实现上述方法
    @Test
    public void method2() throws Exception {
        //1.获取实例
        Class clazz = Class.forName("bull04.Method.Bean");
        Object obj = clazz.newInstance();

        //2.通过获取Bean中的set方法设置数据
        //2.1获得方法,需要明确形参列表(String.class);
        //获取设置姓名方法setName
        Method methodSetName = clazz.getMethod("setName", String.class);
        //获取设置年龄方法setAge
        Method methodSetAge = clazz.getMethod("setAge", int.class);

        //2.2执行方法
        //确定实例对象,并执行确定方法,具体实际参数。
        //invoke可以理解为执行的意思
        methodSetName.invoke(obj, "aaa");//设置姓名
        methodSetAge.invoke(obj, 10);//设置年龄

        System.out.println(obj);
    }

    //在method2的基础上,用get方法获取数据
        @Test
        public void method3() throws Exception {
            //1.获取实例
            Class clazz = Class.forName("bull04.Method.Bean");
            Object obj = clazz.newInstance();

            //2.通过获取Bean中的set方法设置数据
            //2.1获得方法,需要明确形参列表(String.class);
            //获取设置姓名方法setName
            Method methodSetName = clazz.getMethod("setName", String.class);
            //获取设置年龄方法setAge
            Method methodSetAge = clazz.getMethod("setAge", int.class);

            //2.2执行方法
            //确定实例对象,并执行确定方法,具体实际参数。
            //invoke可以理解为执行的意思
            methodSetName.invoke(obj, "aaa");//设置姓名
            methodSetAge.invoke(obj, 10);//设置年龄

            //3.通过get方法获取数据
            //分别获取用来获取姓名和年龄的getName和getAge方法
            //需要注意的是get方法没有形式参数!!!所以不用指定形参类型。
            Method methodGetName = clazz.getMethod("getName");
            Method methodGetAge = clazz.getMethod("getAge");

            //invoke返回的是object对象,所以要进行强制装换。invoke后面括号不用指定参数。
            String str = (String) methodGetName.invoke(obj);
            int age = (int) methodGetAge.invoke(obj);

            System.out.println(str+"---"+age);
        }
}
  • private type method
package bull04.Method;

import java.lang.reflect.Method;

import org.junit.Test;

/*
 * private类型方法
 */
public class PrivateMethod {
    @Test
    public void method () throws Exception {
        //1.获取实例
        Class clazz = Class.forName("bull04.Method.Bean");
        Object obj = clazz.newInstance();

        //2.获取Bean中的私有方法show
        Method showMethod = clazz.getDeclaredMethod("show", int.class);
        //3.强制设置运行访问私有(暴力)
        showMethod.setAccessible(true);
        //4.执行方法
        Object r = showMethod.invoke(obj, 18);//输出  :私有方法:18
        System.out.println(r);//输出:null

    }
}
  • main method
package bull04.Method;

import java.lang.reflect.Method;

import org.junit.Test;

/*
 * main方法
 */
public class MainMethod {
    @Test
    public void method() throws Exception {
        //1.获取Class对象,main方法是静态方法所以不需要获取实例
        Class clazz = Class.forName("bull04.Method.Bean");

        //2.获取main方法
        Method mainMethod = clazz.getMethod("main", String[].class);    

        //3.执行main方法
        /*
         * 参数1:为实例对象(变量名),static方法不需要实例
         * 参数2:main方法执行时的实际参数
         * 可变参数在执行时,JVM内部将传递实参数组打散,变成多个参数
         *      method.invoke(null,args);
         *      method.invoke(null,"abc","123","xxx");
         */
        String[] args = {"abc","123","xxx"};
        //mainMethod.invoke(null, args);//错误写法
        //3.1执行方式一:将String转换为Object,可变参数将不处理数组
        mainMethod.invoke(null, (Object)args);
        //3.2执行方式二:提供二维数组,args将作为二维数组的第一个数
        mainMethod.invoke(null, new Object[] {args});
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325654977&siteId=291194637