java 反射

反射

1. 获取类对象的三种方法

public static void method2() throws ClassNotFoundException {
        //获取类对象方法 1
        Class<Monkey> c1 = Monkey.class;
        //获取类对象方法 2
        Monkey class2 = new Monkey();
        Class<? extends Monkey> c2 = class2.getClass();
        //获取类对象方法 3
        Class<?> c3 = Class.forName("com.deu.javasm.day0310.Monkey");
    }

2. 获取构造器

public static void method3() throws ClassNotFoundException, NoSuchMethodException {
        Class<?> c1 = Class.forName("com.deu.javasm.day0310.Monkey");
        //获取Public修饰的构造器
        Constructor<?>[] constructors = c1.getConstructors();
        //获取某个指定public修饰的构造器
        Constructor<?> constructor = c1.getConstructor();
        //获取所有的构造器包含private
        Constructor<?>[] declaredConstructors = c1.getDeclaredConstructors();
        //获取某个指定的构造器
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor();
    }

3. 用反射操作构造器,创建对象

public static void method4() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取类对象
        Class<?> c1 = Class.forName("com.deu.javasm.day0310.Monkey");
        //获取构造器
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor();
        //创建对象(访问权限)
        declaredConstructor.setAccessible(true);
        Object o = declaredConstructor.newInstance();
        System.out.println(o);
    }

4. 使用反射操作成员变量

public static void method5() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //获取类对象
        Class<?> c1 = Class.forName("com.deu.javasm.day0310.Monkey");
        //获取指定的成员变量
        Field field = c1.getField("name");
        //获取所有public修饰的成员变量(包括从父类继承的)
        Field[] fields = c1.getFields();
        //获取定的成员变量(不管什么权限修饰的)(不包含父类继承的)
        Field declaredField = c1.getDeclaredField("name");
        //获取所有public修饰的成员变量(不管什么权限修饰的)(不包含父类继承的)
        Field[] declaredFields = c1.getDeclaredFields();
        //创建对象
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor();
        Object o = declaredConstructor.newInstance();
        //赋值
        declaredField.setAccessible(true);
        declaredField.set(o, "houzi");
        System.out.println(o);
    }

5. 使用反射 操作方法 调用方法

public static void method6() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //获取类对象
        Class<?> c1 = Class.forName("com.deu.javasm.day0310.Monkey");
        //获取指定的方法 包括父类继承的
        Method method = c1.getMethod("name",String.class);
        //获取所有public修饰的方法 包括父类继承的
        Method[] methods = c1.getMethods();
        //获取指定的方法  (不管什么权限修饰符;不包含继承的)
        Method name = c1.getDeclaredMethod("name", String.class);
        //获取所有的方法  (不管什么权限修饰符;不包含继承的)
        Method[] declaredMethods = c1.getDeclaredMethods();
        //创建对象
        Constructor<?> declaredConstructor = c1.getDeclaredConstructor();
        Object o = declaredConstructor.newInstance();
        //方法调用
        name.setAccessible(true);
        name.invoke(o,"houzi");
        //有关静态方法的调用
        Method name1 = c1.getDeclaredMethod("name", String.class);
        name1.invoke(null,"houzi");
    }

posted @ 2023-03-10 22:34  VIoleng  阅读(17)  评论(0)    收藏  举报