• 反射的应用


    /**

     * 加载类的字节码的3种方式

     * @throws Exception

     * */

    public void test1() throws Exception {

    // 方式一

    Class clazz1 = Class.forName("完整类名");

    // 方式二

    Class clazz2 = Person.class;

    // 方式三

    Person p1 = new Person();

    Class clazz3 = p1.getClass();

    }

    获取构造函数

        

    /**

     * 通过Class对象获取类的一些信息

     *

     * @throws Exception

     * */

    private static void test2() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    // 获取类的名称

    String name = clazz1.getName();

    System.out.println(name); // cn.itcast.gz.reflect.Person

    // 获取类的简单名称

    System.out.println(clazz1.getSimpleName()); // Person

    // 获取类的修饰符

    int modifiers = clazz1.getModifiers();

    System.out.println(modifiers);

    // 构建对象(默认调用无参数构造.)

    Object ins = clazz1.newInstance();

    Person p = (Person) ins;

    System.out.println(p); // cn.itcast.gz.reflect.Person@c17164

    // 获取指定参数的构造函数

    Constructor<?> con = clazz1.getConstructor(String.class, int.class);

    // 使用Constructor创建对象.

    Object p1 = con.newInstance("jack", 28);

    System.out.println(((Person) p1).getName());

    }

    获取方法

      

    /**

     * 获取公有方法.

     * @throws Exception

     * */

    private static void test3() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    // 1.获取非私用方法(包括父类继承的方法)

    Method[] methods = clazz1.getMethods();

    System.out.println(methods.length);

    for (Method m : methods) {

    // System.out.println(m.getName());

    if ("eat".equals(m.getName())) {

    m.invoke(clazz1.newInstance(), null);

    }

    }

    }

      

    /**

     * 获取指定方法签名的方法

     *

     * @throws Exception

     * */

    private static void test4() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    // 获取指定名称的函数

    Method method1 = clazz1.getMethod("eat", null);

    method1.invoke(new Person(), null);

    }

      

    /**

     * 获取指定方法名且有参数的方法

     *

     * @throws Exception

     * */

    private static void test5() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    Method method = clazz1.getMethod("eat", String.class);

    method.invoke(new Person(), "包子");

    }

    /**

     * 获取指定方法名,参数列表为空的方法.

     *

     * @throws Exception

     * */

    private static void test4() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    // 获取指定名称的函数

    Method method1 = clazz1.getMethod("eat", null);

    method1.invoke(new Person(), null);

    }

    /**

     * 反射静态方法

     * @throws Exception

     * */

    private static void test7() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    Method method = clazz1.getMethod("play", null);

    method.invoke(null, null);

    }

    /**

     * 访问私有方法 暴力反射

     * @throws Exception

     * */

    private static void test6() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    Method method = clazz1.getDeclaredMethod("movie", String.class);

    method.setAccessible(true);

    method.invoke(new Person(), "苍老师");

    }

    获取变量

    /**

     * 获取公有的字段

     * */

    private static void test8() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    Field[] fields = clazz1.getFields();

    Person p = new Person();

    System.out.println(fields.length);

    for (Field f : fields) {

    System.out.println(f.getName());

    if ("name".equals(f.getName())) {

    System.out.println(f.getType().getName());

    f.set(p, "jack");

    }

    }

    System.out.println(p.getName());

    }

    /**

     * 获取私有的字段

     * @throws Exception

     * */

    private static void test9() throws Exception {

    Class clazz1 = Class.forName("cn.itcast.gz.reflect.Person");

    Field field = clazz1.getDeclaredField("age");

    System.out.println(field.getName());

    field.setAccessible(true);

    Person p = new Person();

    field.set(p, 100);

    System.out.println(p.getAge());

    }

  • 相关阅读:
    elasticsearch入门 (三 ik 分词器安装)
    elasticsearch入门(二 基础api实例)
    elasticsearch入门(一 基础搭建)
    mysql 错误代码 1045 解决
    信号强度概念
    c语言中,在结构体中如何将void *转存为具体需要的数据类型
    我的论文
    I2C 上拉电阻选择计算公式
    SD卡两种操作模式在项目中应用的比较
    2017 年 年会过后的感想
  • 原文地址:https://www.cnblogs.com/hello-liyb/p/7781116.html
Copyright © 2020-2023  润新知