Java反射的学习笔记

    科技2025-08-06  19

    什么是java反射

    大白话讲就是反向获取class中各种对象的信息、例如成员变量、方法、构造方法、包等等信息

    图片来自:


    获得到类的字节码有3种方式

    要想反射,首先第一步就是得到类的字节码,获得到类的字节码有3种方式

    1、 Student stu1 = new Student();//这一new 产生一个Student对象,一个Class对象。 Class stuClass = stu1.getClass();//获取Class对象 2、 Class stuClass2 = Student.class; 3、 Class stuClass3 = Class.forName("first.Student");//注意此字符串必须是真实路径,就是带包名的类路径,包名.类名 例子如下,先创建一个Student类 package first; public class Student { public String name="aufeng"; //---------------构造方法------------------- //(默认的构造方法) Student(char str){ System.out.println("(默认)的构造方法 s = " + str); } //无参构造方法 public Student(){ System.out.println("调用了公有、无参构造方法执行了。。。"); } //有一个参数的构造方法 public Student(String name){ System.out.println("姓名:" + name); } //有多个参数的构造方法 public Student(String name ,int age){ System.out.println("姓名:"+name+"年龄:"+ age);//这的执行效率有问题,以后解决。 } public void Student1(String name ,int age){ System.out.println("姓名:"+name+"年龄:"+ age);//这的执行效率有问题,以后解决。 } //受保护的构造方法 protected Student(boolean n){ System.out.println("受保护的构造方法 n = " + n); } //私有构造方法 private Student(int age){ System.out.println("私有的构造方法 年龄:"+ age); } } 构造反射代码如下 package first; public class Fanshe { public static void main(String[] args) { //第一种方式获取Class对象 Student stu1 = new Student();//这一new 产生一个Student对象,一个Class对象。 Class stuClass = stu1.getClass();//获取Class对象 System.out.println(stuClass.getName()); //第二种方式获取Class对象 Class stuClass2 = Student.class; System.out.println(stuClass == stuClass2);//判断第一种方式获取的Class对象和第二种方式获取的是否是同一个 //第三种方式获取Class对象 try { Class stuClass3 = Class.forName("first.Student");//注意此字符串必须是真实路径,就是带包名的类路径,包名.类名 System.out.println(stuClass3 == stuClass2);//判断三种方式是否获取的是同一个Class对象 } catch (ClassNotFoundException e) { e.printStackTrace(); } } }


    反射有参构造函数

    Class stuClass3 = Class.forName("first.Student");// 获取类的 Class 对象实例 Constructor c = stuClass3.getConstructor(String.class);// 根据 Class 对象实例获取 Constructor 对象 c.newInstance("abc");

    反射“私有”的构造函数

    Class stuClass3 = Class.forName("first.Student"); Constructor c = stuClass3.getDeclaredConstructor(int.class); c.setAccessible(true); c.newInstance(20);

    反射得到类中所有的构造函数

    Class stuClass3 = Class.forName("first.Student"); Constructor[] cs=stuClass3.getDeclaredConstructors(); for(Constructor c:cs) { System.out.println(c); }

    反射类中的方法

    Class stuClass3 = Class.forName("first.Student"); Student p = (Student)stuClass3.newInstance(); Method m = stuClass3.getMethod("Student1",String.class,int.class);// 获取方法的 Method 对象 m.invoke(p,"aufeng",20);// 利用 invoke 方法调用方法

    也可以直接写成

    stuClass3.getMethod("Student1",String.class,int.class).invoke((Student)stuClass3.newInstance(),"aufeng",20);

    反射类中的属性字段

    Class stuClass3 = Class.forName("first.Student"); Student p =(Student)stuClass3.newInstance(); Field f =stuClass3.getField("name");//反射类中的属性字段 String s=(String)f.get(p); System.out.println(s); f.set(p, "aaaffff");//修改strings值 System.out.println(p.name);

    参考文章 https://blog.csdn.net/ju_362204801/article/details/90578678

    Processed: 0.011, SQL: 8