Java常见应用-反射

  • Post author:
  • Post category:java




反射



定义和功能



定义

反射机制是在

运行状态

中:

对于任何一个类,都能知道这个类的所有属性和方法;

对于任何一个对象,都能调用它的任意一个方法和属性;



功能

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

(2)在运行时构造任意一个类的对象;

(3)在运行时判断任意一个类所具有的成员变量和方法;

(4)在运行时调用任意一个对象的方法;

(5)生成动态代理;



代码测试



基础接口和类

接口

package reflect;

public interface MyInterface {
    void interfaceMethod();
}

package reflect;

public class Person implements MyInterface,Myinterface2{
    private int id;
    private String name;
    private int age;

    public Person(int id) {
        this.id = id;
    }

    public Person() {
    }

    public Person(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public static void personStaticMethod(){
        System.out.println("person类静态方法。。。");
    }

    private void getPrivateMethod(){
        System.out.println("Person类私有方法。。。");
    }


    @Override
    public void interfaceMethod() {
        System.out.println("Person类实现MyInterface接口方法。。。");
    }

    @Override
    public void interfaceMethod2() {

    }
}




通过反射获取类

    //通过反射获取类(3种方式)
    //Class.forName()
    public void getObject1(){
        Class<?> personClass = null;
        try {
            personClass = Class.forName("reflect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("方法1获取person类:"+personClass);
    }

    //XX.class
    public void getObject2(){
        Class<Person> personClass = Person.class;
        System.out.println("方法2获取person类:"+personClass);
    }

    //对象.getClass()
    public void getObject3(){
        Person person=new Person();
        Class<? extends Person> personClass = person.getClass();
        System.out.println("方法3获取person类:"+personClass);
    }



通过反射获取方法

    /**通过反射获取方法**/
    public void getMethod(){
        Class<?> personClass = null;
        try {
            personClass = Class.forName("reflect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        /**可以获取到本类(除private方法外)和父类,接口的全部方法**/
        Method[] methods = personClass.getMethods();
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
        /**可以获取到本类的全部方法**/
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }



通过反射获取接口

    /**通过反射获取接口**/
    public void getInterface(){
        Class<?> personClass = null;
        try {
            personClass = Class.forName("reflect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Class<?>[] interfaces = personClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println(anInterface);
        }
    }



通过反射获取私有属性和方法

 /**通过反射访问私有属性和私有方法**/
    public void getPrivateField(){
        Class<?> personClass = null;
        try {
            personClass = Class.forName("reflect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //创建对象
            Person per = (Person) personClass.newInstance();
            //获取访问属性和方法
            Field id = personClass.getDeclaredField("id");
            Method method = personClass.getDeclaredMethod("getPrivateMethod", null);
            //打开私有属性和私有方法的访问权限
            method.setAccessible(true);
            id.setAccessible(true);
            //赋值
            id.set(per,1);
            //调用方法
            method.invoke(per,null);
            System.out.println(per.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



动态调用

先建立一个class.txt文件,放在项目

最外层目录

在这里插入图片描述

文件内容:

className=reflect.Person
methodName=getPrivateMethod

动态调用代码:

  /**通过反射动态调用**/
    public void dynamicLoading() throws Exception {
        Properties properties=new Properties();
        properties.load(new FileReader("class.txt"));
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");

        Class<?> personClass = null;
        try {
            personClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //创建对象
        Person per = (Person) personClass.newInstance();
        //获取访问属性和方法
        Field id = personClass.getDeclaredField("id");
        Method method = personClass.getDeclaredMethod(methodName, null);
        //打开私有属性和私有方法的访问权限
        method.setAccessible(true);
        id.setAccessible(true);
        //赋值
        id.set(per,1);
        //调用方法
        method.invoke(per,null);
        System.out.println(per.getId());
    }



版权声明:本文为weixin_42339278原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。