反射

反射机制实在【运行状态】中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象,都能够调用它的任意一个方法和属性。
反射提供的功能:
在运行时判断任意一个对象所属的类,在运行时构造任意一个类的对象,在运行时判断任意一个类所具有的成员变量和方法,在运行时调用任意一个对象的方法,生成动态代理。

代码区:

接口1

package refelect;

public interface MyInterface {
    void interfaceMethod();
}

接口2

package refelect;

public interface Myinterface02 {
    void interface2Method();
}

package refelect;

public class Person implements MyInterface,Myinterface02{
    private int id;
    private String name;
    private int age;
    public String desc;
    public void setId(int id) {
        this.id = id;
    }
    private Person(String name){
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }

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

    }

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

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public void interfaceMethod() {
        System.out.println("interfaceMethod....");
    }
    public static void staticMethod(){
        System.out.println("staticMethod");
    }
    private void privateMethod(){
        System.out.println("privateMethod");
    }
    private void privateMethod(String name){
        System.out.println("privateMethod" + name);
    }
    @Override
    public void interface2Method() {
        System.out.println("第二个接口中的方法!");
    }
}

操作类

package refelect;

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

public class ReflectDemo {
    //通过反射来操作类
    public static void TestClass(){
        //获取反射对象(反射入口):
        // 获取Class的三种方法
        // 第一种.Class.forName()
        try {
            Class aClass = Class.forName("refelect.Person");
            System.out.println(aClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //第二种,类名.class
        Class personClass = Person.class;
        System.out.println(personClass);
        //第三种,对象.class()
        Person p = new Person();
        Class pClass = p.getClass();
        System.out.println(pClass);
    }
    //利用反射获取当前类的公共方法
    public static void TestMethod(){
        //Class入口
        Class aClass = null;
        try {
            aClass= Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取所有的   公共   方法,
        //所谓的公共方法,指的是本类,父类,以及接口中的所有方法
        //符合访问权限修饰符规律,也就是public修饰的方法
        Method[] methods = aClass.getMethods();
        //遍历所有的公共方法
        for (Method method: methods) {
            System.out.println(method);
            /*
             * public java.lang.String refelect.Person.getName()
             * 通过反射取出来的方法,会加上完整的包名
             * 通过反射取方法,不仅仅取到了自己类中的方法,还取到了父类的方法,也就是Object类中的方法
             * */
        }
    }
    //获取所有的接口,java单继承多实现
    public static void Demo03(){
        //Class入口
        Class aClass =null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取所有的接口
        Class[] interfaces = aClass.getInterfaces();
        //遍历接口
        for (Class inter : interfaces) {
            System.out.println(inter);
        }
    }
    //通过反射来获取类的父类
    public static void Demo04(){
        //入口
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //java是单继承多实现,所以父类只有一个
        Class superclass = aClass.getSuperclass();
        System.out.println("Person的父类是:" + superclass);
    }
    //通过反射获取该类的所有构造方法
    public static  void Demo05(){
        //入口
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取所有的构造方法
        Constructor[] constructors = aClass.getConstructors();
        for (Constructor con:constructors) {
            System.out.println(con);
        }

    }
    //通过反射获取所有的公共属性
    public static void Demo06(){
        //入口
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取所有的公共属性
        //所谓的公共属性,指的是本类以及父类中的公共属性
        //符合访问权限修饰符规律,也就是public修饰的成员变量
        Field[] fields = aClass.getFields();
        for (Field f:fields) {
            System.out.println(f);
        }
    }
    //通过反射获取当前类的所有方法,其中包含公共方法和私有方法,不包含父类和父接口
    public static void Demo07(){
        //入口
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取当前类的所有方法,忽略访问权限修饰符的限制
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method me:declaredMethods) {
            System.out.println(me);
        }
    }
    //获取当前类的所有构造方法,忽略访问权限修饰符
    public static void Demo08(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Constructor[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor c:declaredConstructors) {
            System.out.println(c);
        }
    }
    //获取当前类的所有属性,忽略权限修饰符
    public static void Demo09(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field f:declaredFields) {
            System.out.println(f);
        }
    }
    //通过反射来new一个对象
    public static void Demo10(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        o1.interfaceMethod();
    }
//通过反射对对象中的属性赋值
    public static void Demo11(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        o1.setName("路人甲");
        o1.setAge(22);
        System.out.println(o1.getName() + "今年" + o1.getAge());
    }
    //通过反射操作属性
    public static void Demo12(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        Field name = null;
        try {
            name = aClass.getDeclaredField("name");
            //访问的是private修饰的id,但是private是私有的
            //所以我们需要修饰访问权限修饰符
            //设置访问权限修饰符,将private改为public
            name.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        try {
            name.set(o1,"张三");

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        System.out.println(o1.getName());
    }
    //通过反射对私有方法进行调用
    public static void Demo13(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        Method privateMethod = null;
        try {
            privateMethod = aClass.getDeclaredMethod("privateMethod", null);
            privateMethod.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            privateMethod.invoke(o1,null);//方法的调用通过invoke();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    //通过反射对有参私有构造赋值
    public static void Demo14(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = aClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        Method privateMethod = null;
        try {
            privateMethod = aClass.getDeclaredMethod("privateMethod", String.class);
            privateMethod.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            privateMethod.invoke(o1,"张三");//方法的调用通过invoke();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    //通过反射获取指定的公共方法
    public static void Demo15(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Constructor constructor = null;
        try {
            constructor = aClass.getConstructor(int.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        System.out.println(constructor);
    }
    //通过反射获取指定的私有构造方法
    public static void Demo16(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Constructor declaredConstructor = null;
        try {
            declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        System.out.println(declaredConstructor);
    }
    //通过反射获取指定的私有构造方法并实例化
    public static void Demo17(){
        Class aClass = null;
        try {
            aClass = Class.forName("refelect.Person");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Constructor declaredConstructor = null;
        try {
            declaredConstructor = aClass.getDeclaredConstructor(String.class);
            declaredConstructor.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        Object o = null;
        try {
            o = declaredConstructor.newInstance("张三");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        Person o1 = (Person) o;
        System.out.println(o1.getName());
    }
}

你可能感兴趣的:(反射)