Java反射的三种实现方式,访问私有方法和私有属性

反射允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,Spring就是通过反射来实现依赖注入的。


package com.main;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {

    public static void main(String[] args) {
        //new  对象
        Test test = new Test();
        test.f1();

        //方式1  实例.getClass()
        try {
            Class test1 = test.getClass();
            Test test11 = (Test) test1.newInstance();
            test11.f1();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        //方式2  Class.forName("类的路径")
        try {
            Class test2 = Class.forName("com.main.Test");
            Test test21 = (Test) test2.newInstance();
            test21.f1();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        //方式3  类名.class
        Class test3 = Test.class;
        try {
            Test test31 = (Test) test3.newInstance();
            test31.f1();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //获得包名
        System.out.println(test3.getCanonicalName());//com.main.Test
        //获得类名
        System.out.println(test3.getSimpleName());//Test


        //访问私有方法
        Method method = null;
        try {
            method = test3.getDeclaredMethod("helloF2",
                    new Class[]{String.class});
            method.setAccessible(true); // 抑制Java的访问控制检查
            // 如果不加上上面这句,将会Error: TestPrivate can not access a member of class PrivateClass with modifiers "private"
            String str = (String) method.invoke(test, new Object[]{"zhangsan"});
            System.out.println(str);//输出:hello:zhangsan
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


        //访问私有属性
        Field field = null;
        try {
            field = test3.getDeclaredField("name");
            field.setAccessible(true); // 抑制Java对修饰符的检查
            field.set(test, "lisi");
            System.out.println(test.getName());//输出:lisi
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }//main
}

//Test---f1
//Test---f1
//Test---f1
//Test---f1
//com.main.Test
//Test
//hello:zhangsan
//lisi

package com.main;

/**
 * Created by liuyazhou on 2017/9/23.
 */
public class Test {

    private String name;

    public void f1() {
        System.out.println("Test---f1");
    }

    private String helloF2(String str) {
        return "hello:" + str;
    }


    public String getName() {
        return name;
    }
}












你可能感兴趣的:(Java,面试)