zoukankan      html  css  js  c++  java
  • Java 反射机制 ( Java Reflection Mechanism )

    Example

    @Data
    public class Student {
      
        private int age;
    }
    
    public class Reflection {
    
        public static void main(String[] args) throws Exception {
          	// 正常调用
            Student student = new Student();
            student.setAge(18);
            System.out.println(student.getAge());
    
            // Reflection
            Class studentClass = Class.forName("com.git.Reflection.Student");
          
            // get method
            Method setAge = studentClass.getMethod("setAge", int.class);
            Method getAge = studentClass.getMethod("getAge");
          
          	// Constructor
            Constructor studentClassConstructor = studentClass.getConstructor();
          
          	// new 一个 Object
            Object o = studentClassConstructor.newInstance();
          
            setAge.invoke(o, 18);
          	Object age = getAge.invoke(o);
            System.out.println(age);
        }
    }
    

    执行结果完全是一样的,区别就在,

    第一段在代码未执行的时就已经确定了要运行的类(Student)

    第二段代码则是在运行的时候通过类的全类名确定要运行的类 (Student)

    反射就是在代码运行的时候才确定需要执行的类并且可以拿到其所有的方法

    反射常用的 API

    获取反射中的Class对象
    • Class.forName 需要类的全类名

      Class studentClass = Class.forName("com.java.Reflection.Student");
      
    • Object.class

      Class<Student> studentClass = Student.class;
      
    • Object.getClass() 类对象的 getClass() 方法

      String s = new String("qqq");
      Class<? extends String> aClass1 = s.getClass();
      
    通过反射创建对象
    • Class对象的 newInstance()方法
    Class<Student> aClass = Student.class;
    Student student = constructor.newInstance();
    
    • ConstructornewInstance()方法
    Class<Student> studentClass = Student.class;
    Constructor<Student> constructor = studentClass.getConstructor();
    Student student = constructor.newInstance();
    
    通过反射获取类属性、方法、构造器
    • 非私有属性

      Class studentClass = Student.class;
      Field[] fields = studentClass.getFields();
      
      Constructor constructor = studentClass.getConstructor();
      System.out.println(constructor.toString());
      
      for (Field field : fields) {
      System.out.println(field.getName());
      }
      

      输出结果

      public com.java.Reflection.Student()
      

      没有输出属性,原因是 Student就没有非私有的属性,获取私有的属性需要用到关键字 declared

    • 全部属性

    Class studentClass = Student.class;
    Field[] fields = studentClass.getDeclaredFields();
    
    Constructor declaredConstructor = studentClass.getDeclaredConstructor();
    System.out.println(declaredConstructor.toString());
    
    for (Field field : fields) {
    System.out.println(field.getName());
    }
    

    输出结果

    public com.ali.Reflection.Student()
    age
    
  • 相关阅读:
    IDEA中快速排除maven依赖
    Maven构建war项目添加版本号
    运行shell脚本报/bin/bash^M: bad interpreter错误排查方法
    Shell杀tomcat进程
    根据URL下载文件
    关闭Centos的自动更新
    CentOS下建立本地YUM源并自动更新
    为Linux服务器伪装上Windows系统假象
    ServerInfo.INI解密
    请教给终端推销域名的邮件该怎么写?
  • 原文地址:https://www.cnblogs.com/hellojava404/p/13701059.html
Copyright © 2011-2022 走看看