反射的应用


public class Person {
    private int age;
    private String name;
    public Person() {
        super();
        System.out.println("person run");
    }
    public Person( String name, int age) {
        super();
        this.age = age;
        this.name = name;
        System.out.println("person param run"+this.name+":"+this.age);
    }
    public void show(){
        System.out.println(name+"....show run...."+age);
    }

    public void method(){
        System.out.println("method run");
    }

    public void paramMethod(String str,int num){
        System.out.println("paramMethod run..."+str+":"+num);

    }
    public static void staticMethod(){
        System.out.println("static method run.....");
    }

}

获取Class中的构造函数

public class four1 {
    public static void main(String[] args) throws Exception {
        //createNewObject_2();

         createNewObject() ;
    }
    public static void createNewObject_2() throws Exception{
        bean.Person p=new bean.Person("小强",39);
        /*
         当获取知道名称对应类中的所体现的对象时,
         而该对象初始化不使用空参数构造函数该怎么办呢?
         既然是通过知道的构造函数进行对象的初始化,
         所以应该先获取该构造函数。通过字节码文件对象即可完成。
         该方法是:getConstructor(paramterTypes);

         * */
        String name="bean.Person";
        //寻找该名称类文件,并加载进内存,并产生Class对象。
        Class clazz=Class.forName(name);
        //获取指定的构造函数对象。
        Constructor constructor=clazz.getConstructor(String.class,int.class);

        //通过该构造器对象的newInstance方法进行对象的初始化
        Object obj=constructor.newInstance("小明",38);




    }

    public static void createNewObject() throws ClassNotFoundException, InstantiationException, IllegalAccessException{
        //早期:new时候,先根据被new的类的名称找寻该类的字节码文件,并加载进内存,
        //并创建该字节码文件对象,并接着创建该字节文件的对应的Person对象。
        bean.Person p=new bean.Person();

        //现在:
        String name="bean.Person";
        //找寻该名称类文件,并加载进内存,并产生Class对象。
        Class clazz=Class.forName(name);
        //该如何产生该类的对象呢?
        Object obj=clazz.newInstance();
    }
}

获取Class中的字段

public class five1 {
    public static void main(String[] args) throws Exception {
        getFieldDemo();
    }

    /*
        获取字节码文件中的字段。
     * */
    public static void getFieldDemo() throws Exception{
        Class clazz=Class.forName("bean.Person");
        Field field=null;//clazz.getField("age");//只能获取公有的。
        field = clazz.getDeclaredField("age");//只能获取本类,但包含私有

        //对私有字段的访问取消权限检查,暴力访问
        field.setAccessible(true);


        Object obj=clazz.newInstance();

        field.set(obj, 89);

        //get方法是获取当前属性的值,这个方法要求我们提供一个参数,
        //而这个参数必须是当前类的实例
        Object o=field.get(obj);


//      bean.Person p=new bean.Person();
//      p.show()


        System.out.println(o);
    }
}

获取Class中的方法

    public class six1 {
    public static void main(String[] args) throws Exception {
        getMethodDemo_2();
    }

    public static void getMethodDemo_3() throws Exception{
        Class clazz=Class.forName("bean.Person");
        Method method=clazz.getMethod("paramMethod", String.class,int.class);//获取有参的方法
        Object obj=clazz.newInstance();
        method.invoke(obj, "小强",89);
    }



    public static void getMethodDemo_2() throws Exception{
        Class clazz=Class.forName("bean.Person");
        Method  methods=clazz.getMethod("show",null);//获取空参数的一般方法
        Object obj=clazz.newInstance();
//      Constructor constructor=clazz.getConstructor(String.class,int.class);
//      Object obj=constructor.newInstance("小名",37);
        methods.invoke(obj, null);
    }



    public static void getMethodDemo() throws Exception{
        Class clazz=Class.forName("bean.Person");
       Method[] methods=clazz.getMethods();//获取的都是公有的方法

      methods=clazz.getDeclaredMethods();//获取本类的中的所有方法,包括私有的

       for (Method method : methods) {
            System.out.println(method);
    }

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值