`

Java 反射

    博客分类:
  • JAVA
 
阅读更多

一、实例化Class类对象的三种方式

1、Class<?> c1 = Class.forName("com.test.Test");//

2、Class<?> c2 = Test.class; //通过类.class实例化

3、Class<?> c3 = new Test().getClass(); //通过Object类中的方法实例化 

二、通过 Class 获取类属性字段

Field[] fields = c1.getFields();

三、获取方法

Method[] methods = c1.getMethods();

四、通过PropertyDescriptor获取方法

Test test=  new Test();
test.setName("12342567890");
PropertyDescriptor prop = new PropertyDescriptor("name", Test.class); 
Method readMethod = prop.getReadMethod();
Object invoke = readMethod.invoke(test);
System.out.println(invoke.toString());

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics