美文网首页
Java反射机制

Java反射机制

作者: jesse_cheng | 来源:发表于2019-01-17 21:55 被阅读0次

1 定义
在运行过程中,可以动态的调用任意一个类的属性和方法。

2 反射的好处
一般分为静态编译、动态编译两种。
静态编译:在编译时确定类型,绑定对象,即通过。
动态编译:运行时确定类型,绑定对象。动态编译最大限度发挥了java的灵活性,体现了多态的应用,用以降低类之间的藕合性。反射即这里的动态编译方式。

3 反射的主要操作
主要操作包括四个部分:获取类操作;获取属性字段操作;获取方法操作;获取构造方法操作。每一类的操作,如下:

/**
 * 反射知识点学习
 */
public class ReflectDemo {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException
    {
        //获取类名称
        getReflectClassName();

        //获取类方法
        getReflectClassMethodName();

        //获取类域名
        getReflectClassFieldName();

        //获取构造方法
        getReflectClassConstructFunction();
    }

    @SuppressWarnings("unchecked")
    private static void getReflectClassName() throws InstantiationException, IllegalAccessException
    {
        Class<ReflectDemo> class1 = ReflectDemo.class;
        System.out.println("class1: " + class1);

        try {
            Class<ReflectDemo> class2 = (Class<ReflectDemo>) Class.forName("com.jesse.learn.reflect.ReflectDemo");
            System.out.println("class2: " + class2);
        }
        catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException");
        }

        ReflectDemo reflectDemo = ReflectDemo.class.newInstance();
        Class<ReflectDemo> class3 = (Class<ReflectDemo>) reflectDemo.getClass();
        System.out.println("class3: " + class3);
    }

    private static void getReflectClassMethodName() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InvocationTargetException {
        Class<Student> class1 = Student.class;

        //reflecting all the declared methods of the class or interface represented by this Class object,
        //including public, protected, default (package) access, and private methods,
        //but excluding inherited methods
        for (Method method : class1.getDeclaredMethods()) {
            System.out.println("getDeclaredMethods: " + method.getName());
        }

        //reflecting all the public methods of the class or interface represented by this Class object,
        //including those declared by the class or interface
        //and those inherited from superclasses and superinterfaces
        for (Method method : class1.getMethods()) {
            System.out.println("getMethods: " + method.getName());
        }

        Student stu = class1.newInstance();
        Method method1 = class1.getMethod("setName", String.class);
        method1.invoke(stu, "wanghairong");
        System.out.println(stu.getName());

        Method method2 = class1.getMethod("getName");
        System.out.println(method2.invoke(stu));

        Method method3 = class1.getMethod("getAge");
        System.out.println(method3.invoke(stu));
    }

    private static void getReflectClassFieldName() throws NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException
    {
        Class<Student> class1 = Student.class;

        //all the accessible public fields of the class or interface represented by this Class object.
        for (Field field : class1.getFields()) {
            System.out.println("Fields: " + field.getName());
        }

        //reflecting all the fields declared by the class or interface represented by this Class object.
        //This includes public, protected, default (package) access, and private fields, but excludes inherited fields
        for (Field field : class1.getDeclaredFields()) {
            System.out.println("DeclaredFields: " + field.getName());
        }

        Field field1 = class1.getDeclaredField("name");
        Student stu = class1.newInstance();
        stu.setName("chengyingjie");

        field1.setAccessible(true);  //私有变量必须设置访问权限,否则无法直接访问
        System.out.println("name: " + field1.get(stu));
    }

    private static void getReflectClassConstructFunction() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

        Class<Student> class1 = Student.class;
        for (Constructor<?> constructor : class1.getConstructors()) {
            System.out.println("Constructors: " + constructor.getName());
        }

        for (Constructor<?> constructor : class1.getDeclaredConstructors()) {
            System.out.println("DeclaredConstructors: " + constructor.getName());
        }

        //注意:如果声明了带整型参数的构造函数,其参数必须声明为Integer,否则,因int不是一个包装类型,反射构建时会报错
        Constructor<Student> constructor = class1.getConstructor(String.class, Integer.class);
        System.out.println("Constructor: " + constructor.getName());

        Student stu2 = constructor.newInstance("chengwang", 20);
        System.out.println("constructorInstance: " + stu2.getName() + ":" + stu2.getAge());
    }
}

相关文章

  • Java反射机制入门

    Java反射机制入门 一、什么是反射 JAVA反射机制(The JAVA reflection mechanism...

  • 反射之一

    总结内容源自一下文章粗浅看java反射机制反射机制应用实践谈谈java反射机制Java Reflection(反射...

  • 反射之二

    总结内容源自一下文章粗浅看java反射机制反射机制应用实践谈谈java反射机制Java Reflection(反射...

  • Java基础之反射

    Java基础之—反射(非常重要)Java中反射机制详解Java进阶之reflection(反射机制)——反射概念与...

  • 反射之三

    总结内容源自以下文章 粗浅看java反射机制 反射机制应用实践 谈谈java反射机制 Java Reflectio...

  • java反射机制

    java的反射机制 1 JAVA的反射机制是什么?反射机制能做什么?反射机制的优点与缺点2 认识 Class...

  • Java中反射的用途

    Java的反射机制是Java特性之一,反射机制是构建框架技术的基础所在。灵活掌握Java反射机制,对大家以后学习框...

  • Chapter 13 . 反射机制

    阅读原文 Chapter 13 . 反射机制 13.1 Java反射机制研究及应用 Java Reflection...

  • 详解Java反射机制(Reflection)

    详解Java反射机制(Reflection) 反射机制的作用 JAVA反射机制是在运行状态中,对于任意一个类,都能...

  • Java 反射机制

    [1]. java反射详解[2]. Java Reflection(反射机制)详解[3]. 深入理解Java类型...

网友评论

      本文标题:Java反射机制

      本文链接:https://www.haomeiwen.com/subject/apmwdqtx.html