反射入门

本文探讨了Java反射机制在面试中常见的加载阶段,并通过示例展示了如何使用反射创建对象、调用方法和访问成员变量。同时,通过对比传统方法和反射调用的性能,分析了未经优化和优化后的反射调用的效率差异,强调了`setAccessible(true)`在性能调优中的作用。
摘要由CSDN通过智能技术生成

 面试常问编译->加载阶段,应用常问运行->加载阶段

re.properties(src包下)
classfullpath=Reflection.Cat
//method=hi
method=cry
Cat
package Reflection;

/**
 * @author whlie(true){learn}
 */
public class Cat {
    private String name="招财猫";
    public  int age=1;
    public Cat(){}
    public Cat(String name){
        this.name=name;
    }
    public void hi(){
        System.out.println("我是"+name);
    }
    public void cry(){
        System.out.println("你是"+name);
    }
}
Ocp
package Reflection;

import java.io.FileInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author whlie(true){learn}
 */
public class Ocp {
    public static void main(String[] args) throws Exception {
        //使用该方法读取配置文件
        Properties p = new Properties();
        p.load(new FileInputStream("src\\re.properties"));
        String classfullpath = p.get("classfullpath").toString();
        String method = p.get("method").toString();

        //反射机制
        //(1)加载类,返回Class类型的对象cls
        Class cls = Class.forName(classfullpath);
        //(2)通过cls得到你加载的类com.wzg.Cat的对象实例
        Object o = cls.newInstance();
        System.out.println("o的运行类型"+o.getClass());
        //(3)通过cls得到你加载的类com.wzg.Cat的method"hi"的方法对象
        //   即在反射中可以视方法为对象
        Method method1 = cls.getMethod(method);
        //(4)通过method1调用方法即通过方法对象来实现调用方法
        //   传统:对象.方法() 反射:方法.invoke(对象)
        method1.invoke(o);
        //(5)获取指定类的成员变量
        //   传统:对象.成员变量 反射:成员变量对象.get(对象)
        Field age = cls.getField("age");
        System.out.println("你的年龄是:"+age.get(o));
        //(6)获取指定类的构造器,如果()里为空则为无参构造器,int类的class对象
        Constructor constructor = cls.getConstructor(String.class);
        System.out.println(constructor);
    }
}

反射与传统方法相比,调优

 

package Reflection;

import java.lang.reflect.Method;

/**
 * @author whlie(true){learn}
 */
public class Tuning {
    public static void main(String[] args) throws Exception {
        t1();
        t2();
        t3();
    }
    //传统方法
    public static void t1(){
        Cat cat = new Cat();
        long start =System.currentTimeMillis();
        for (int i = 0; i < 900000000; i++) {
            cat.hi();
        }
        long end = System.currentTimeMillis();
        System.out.println("传统方法耗时:"+(end-start));
    }
    //反射(未调优)
    public static void t2() throws Exception {
        Class cls = Class.forName("Reflection.Cat");
        Object o = cls.newInstance();
        Method hi = cls.getMethod("hi");

        long start =System.currentTimeMillis();
        for (int i = 0; i < 900000000; i++) {
            hi.invoke(o);
        }
        long end = System.currentTimeMillis();
        System.out.println("反射(未调优)耗时:"+(end-start));
    }
    //反射(调优后)
    public static void t3() throws Exception {
        Class cls = Class.forName("Reflection.Cat");
        Object o = cls.newInstance();
        Method hi = cls.getMethod("hi");
        //在反射调用方法时取消检查
        hi.setAccessible(true);
        long start =System.currentTimeMillis();
        for (int i = 0; i < 900000000; i++) {
            hi.invoke(o);
        }
        long end = System.currentTimeMillis();
        System.out.println("反射(调优后)耗时:"+(end-start));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值