欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Java高级Day41-反射入门

Java高级Day41-反射入门

2025/4/23 14:37:41 来源:https://blog.csdn.net/2301_78630849/article/details/142266280  浏览:    关键词:Java高级Day41-反射入门

115.反射

反射机制

1.根据配置文件re.properties指定信息,创建Cat对象并调用hi方法

@SuppressWarnings({"all"})
public class ReflectionQuestion {public static void main(String[] args) throws IOException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件Properties properties = new Properties();properties.load(new FileInputStream("src\\re.properties"));String classfullpath = properties.get("classfullpath").toString();Object method = properties.get("method").toString();System.out.println("classfullpath=" + classfullpath);System.out.println("method=" + method);
​//2.创建对象,传统的方法行不通 -> 反射机制//new classfullpath();}
}

2.这样的需求在学习框架的时候特别多,即通过外部文件配置,在不修改源代码的情况下,来控制程序,也符合设计模式的ocp原则(开闭原则)

反射快速入门
public class ReflectionQuestion {public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {//根据配置文件 re.properties 指定信息,创建Cat对象并调用方法hi//传统的方式 new 对象 -> 调用方法
//        Cat cat = new Cat();
//        cat.hi();
​//1.使用Properties类,可以肚饿写配置文件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=" + classfullpath);System.out.println("method=" + methodName);
​//2.创建对象,传统的方法行不通 -》 反射机制//new classfullpath();
​//3.使用反射机制//(1)加载类,返回Class类型的对象Class cls = Class.forName(classfullpath);//(2) 通过cls 得到你加载的类 com.reflection.question.Cat 的对象实例Object o = cls.newInstance();System.out.println("o的运行类型=" + o.getClass()); //运行类型//(3) 通过 cls 得到你加载的类 com.reflection.question.Cat 的 mothName 的方法对象//    即:在反射中,可以把方法视作对象(万物皆对象)Method method1 = cls.getMethod(methodName);//(4) 通过method1 调用方法:即通过方法对象来实现调用System.out.println("=================");method1.invoke(o);//传统方法 对象.方法() , 反射机制 方法.invok(对象)}
}
反射原理图
  1. 反射机制允许程序在执行期借助于ReflectionAPI取得任何类的内部信息(比如成员变量,构造器,成员方法等等),并能操作对象的属性及方法。反射在设计模式和框架底层都会用到

  2. 加载完类之后,在堆中就产生了一个Class类型的对象(一个类只有一个Class对象),这个对象包含了类的完整结构信息。通过这个对象得到类的结构。这个对象就像一面镜子,透过这个镜子看到类的结构,所以称之为反射

反射相关类

Java反射机制可以完成

  1. 在运行时判断任意一个对象所属的类

  2. 在运行时构造任意一个类的对象

  3. 在运行时得到任意一个类所具有的成员变量和方法

  4. 在运行时调用任意一个对象的成员变量和方法

  5. 生成动态代理

反射相关的主要类

  1. java.lang.Class:代表一个类,Class对象标识某个类加载后在堆中的对象

  2. java.lang..reflect.Method:代表类的方法,Method对象表示某个类的方法

  3. java.lang..reflect.Field:代表类的成员变量,Field对象标识某个类的成员变量

    //得到name字段
    //getField不能得到私有的属性 
    Field name = cls.getField("name");
    System.out.println(nameField.get(o));//传统写法 对象.成员变量 , 反射:成员变量对象.get(对象)  
  4. java.lang..reflect.Constructor:代表类的构造方法,Constructor表示一个构造器

    Constructor constructor = cks.getConstructor();//()中可以指定构造器参数类型,返回无参构造器
    System.out.println(constructor);//Cat()
    ​
    Constructor constructor2 = cls.getConstructor(String.class);//这里传入的String.class就是String类型的Class对象
    System.out.println(constructor2);//Cat(String name)
反射调用优化

反射优点和缺点

优点:可以动态的创建和使用对象(也是框架底层核心),使用灵活,没有反射机制,框架技术就数去底层支撑

缺点:使用反射基本是解释执行,对执行速度有影响

public class ReflectionQuestion {public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException {m1();m2();}//传统方法调用hi
​public static void m1(){long start = System.currentTimeMillis();Cat cat = new Cat();for (int i = 0;i < 90000000;i++){cat.hi();}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//3}
​//反射机制调用方法hipublic static void m2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class<?> cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("传统方法来调用hi的时间 耗时" + (end - start));//286}
}

优化-关闭访问检查

  1. Method和Field、Constructor对象都有setAccessible()方法

  2. setAccessible作用是启动和禁用访问安全检查的开关

  3. 参数值为true表示 反射的对象在使用时取消访问检查,提高反射的效率,反射值为false则表示反射的对象执行访问检查

//反射优化public static void m3() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {Class cls = Class.forName("reflection.question.Cat");Object o = cls.newInstance();Method hi = cls.getMethod("hi");hi.setAccessible(true);//优化long start = System.currentTimeMillis();for (int i = 0;i < 90000000;i++){hi.invoke(o);//反射调用方法}long end = System.currentTimeMillis();System.out.println("m3方法来调用hi的时间 耗时" + (end - start));//115}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词