反射

作者: woochen123 | 来源:发表于2017-09-04 22:37 被阅读0次

实体类

public class TestBean {
    String name;
    int age;
    private TestBean() {

    }

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

    public String getName(){
        return name;
    };

    public void setName(String name){
          this.name = name;
    }; 
    private int  getAge(){
        return age;
    }
}

反射获取实例

//无参构造(私有)
TestBean testBean = TestBean.class.newInstance();
//无参构造(公有)
Constructor<TestBean> constructor = TestBean.class.getConstructor();
TestBean testBean = constructor.newInstance();
//无参构造(私有)
Constructor<TestBean> constructor = TestBean.class.getDeclaredConstructor();
constructor.setAccessible(true);
TestBean testBean = constructor.newInstance();
//有参构造(私有)
Constructor<TestBean> constructor = TestBean.class.getDeclaredConstructor(String.class, int.class);
constructor.setAccessible(true);
TestBean testBean = constructor.newInstance("woochen123", 23);

反射获取方法

//获取方法(公有)
//参数1:方法名      参数2:方法参数类型的class对象(集合)
Method method = TestBean.class.getMethod("setName",String.class);
//参数1:执行方法的对象(静态方法可以传null)      参数2:方法需要的参数
method.invoke(testBean,"woochen123");

//获取方法(私有)
Method method = TestBean.class.getDeclaredMethod("getAge");
method.setAccessible(true);
method.invoke(testBean,null);

//补充
declaredMethod.getReturnType()   //获得返回值
declaredMethod.getGenericReturnType() //获得完整信息的返回值
 Class<?>[] parameterTypes = declaredMethod.getParameterTypes(); //获得参数类型
Class<?>[] exceptionTypes = declaredMethod.getExceptionTypes();     //获得异常名称
 Annotation[] annotations = declaredMethod.getAnnotations(); //获得注解

反射获取属性

//属性(公有)
 Field field = TestBean.class.getField("name");
//获取属性   参数:属性所在的对象(如果是静态属性可以传null)
 String name = (String) field.get(testBean);
//设置属性 参数1:属性所在的对象(如果是静态属性可以传null)  参数2:给属性赋的值
field.set(testBean,"woochen123");

//属性(私有)
Field field = TestBean.class.getDeclaredField("name");
field.setAccessible(true);
String name = (String) field.get(testBean);
field.set(testBean,"woochen123");

//补充
field.getType():返回这个变量的类型
field.getGenericType():如果当前属性有签名属性类型就返回,否则就返回 Field.getType()

补充

  • testBean.getClasses()
    返回调用类的所有公共类、接口、枚举组成的 Class 数组,包括继承的
  • testBean.getModifiers()
    获得调用类的修饰符的二进制值
  • Modifier.toString(int modifiers)
    将二进制值转换为字符串

注意

  • 只有被 @Retention(RetentionPolicy.RUNTIME) 修饰的注解才可以在运行时被反射获取

相关文章

  • Java基础之反射

    Java基础之反射 反射基本介绍 反射的使用通过反射调用属性和方法通过反射获取配置文件 反射基本介绍 Java反射...

  • 镜面反射矩阵的推导

    镜面反射是以镜面作为反射平面,实物与反射物到反射平面的距离相等,实物与反射物方向相反,所以,反射矩阵由反射平面确定...

  • reflect.go包学习_之二 指针操作提高反射性能 反射应用

    reflect.go包学习_之二 指针操作提高反射性能 反射应用 反射创建实例 反射信息、反射调用方法、反射修改值...

  • Java互联网公司-经典面试题附答案

    基础:Java 反射?反射有什么缺点?你是怎么理解反射的(为什么框架需要反射)?优点:反射具有解耦性,缺点:反射属...

  • Java反射与joor反射库的使用

    java原生反射的使用 反射构造对象 反射方法 反射字段 joor反射库的使用 github:https://gi...

  • Java反射

    什么是反射? 反射的作用? 反射性能优化?

  • 反射三定律

    反射第一定律:反射可以将interface类型变量转换成反射对象 反射第二定律:反射可以将反射对象还原成inter...

  • 反射

    1.反射是什么?反射的定义,概念 2.为什么要学反射? 3.怎么使用反射?反射的使用

  • 一周岁前做好两件事,孩子就不会语言迟缓,保证口齿伶俐

    与语言发展相关的原始反射有四个:张口反射、足跖反射、抓握反射和手拉反射,每个反射的发生、发展和整合都是次第进行的,...

  • 面试官问go反射第一弹

    目录 反射概念 reflect包 反射类型(Type)和种类(Kind) 反射类型(Type)使用 反射类型对象(...

网友评论

      本文标题:反射

      本文链接:https://www.haomeiwen.com/subject/rafyjxtx.html