java反射reflection------反射机制(一)

目录

反射的引入 

反射原理图

 反射相关类

反射调优 


 

反射的引入 

public class Cat {
    private String name="招财猫";
    public Integer age=10;


    public Cat() {
    }

    public Cat(String name) {
        this.name = name;
    }

    public void hi(){
        //System.out.println("hi"+name);
    }
}

 

//1、传统的方式--new
        /*Cat cat = new Cat();
        cat.hi();*/

        //2、反射的引入
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\re.properties"));
        //得到全路径
        String classfullpath = properties.get("classfullpath").toString();
        String methodName = properties.get("method").toString();
        System.out.println(classfullpath);
        System.out.println(methodName);

        //创建对象
        //通过外部文件的配置 在不修改源码的情况下 来控制程序,也符合设计模式的ocp原则
        //ocp:开闭原则:不修改源码,扩展功能

        //3、快速入门
        //(1)加载类,返回Class类型的对象
        Class clazz = Class.forName(classfullpath);
        //(2)通过clazz 得到全路径对应类的一个实例
        System.out.println(clazz.newInstance().getClass());//得到运行类型--class com.demo.Cat
        Cat cat = (Cat)clazz.newInstance();
        //(3)得到加载类的对应方法
        // ----在反射中 可以把方法视为对象
        Method method = clazz.getMethod(methodName);
        //(4) 通过方法的调用对象调用方法
        System.out.println("=====================");
        method.invoke(cat);
        //传统方法: 对象.方法(),反射机制 方法.invoke(对象)

反射原理图

 

 反射相关类

 

 

//3、getFiled不能得到私有属性
        //Field name = clazz.getField("name");//会报错
        Field age = clazz.getField("age");
        System.out.println(age);
        //得到的是public java.lang.Integer com.demo.Cat.age
        //要想得到值 传统:对象.成员变量;反射:字段成员对象.get(对象)

        System.out.println(age.get(cat));//10
        //4、getConstructor()
        //无参构造器
        Constructor constructor = clazz.getConstructor();
        System.out.println(constructor);
        //返回public com.demo.Cat()一个无参构造器

        //有参构造器-传入一个形参类型的class对象
        Constructor constructor1 = clazz.getConstructor(String.class);
        System.out.println(constructor1);
        //输出public com.demo.Cat(java.lang.String)

反射调优 

 

 

 public static void main(String[] args) throws Exception {

        m1();
        m2();
        m3();
    }

    //传统方法调用hi
    public static void m1(){
        Cat cat = new Cat();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 9000000; i++) {
            cat.hi();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("传统方法耗时:"+(endTime-startTime));
    }

    //反射机制hi
    public static void m2() throws Exception {
        Class clazz = Class.forName("com.demo.Cat");
        Object o = clazz.newInstance();
        Method hi = clazz.getMethod("hi");
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 9000000; i++) {
            hi.invoke(o);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射耗时:"+(endTime-startTime));
    }

    //反射优化=关闭访问检查
    public static void m3() throws Exception {
        Class clazz = Class.forName("com.demo.Cat");
        Object o = clazz.newInstance();
        Method hi = clazz.getMethod("hi");
        hi.setAccessible(true);
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 9000000; i++) {
            hi.invoke(o);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("反射优化耗时:"+(endTime-startTime));
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值