码迷,mamicode.com
首页 > 其他好文 > 详细

反射技术获取到对象中的指定方法

时间:2018-03-19 10:22:59      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:反射技术获取到对象中的指定方法

使用反射技术根据路径获取到对象
对象.Class获取到class对象
使用class对象通过getDeclaredFields()方法获取到目标类中的属性值集合Field[]
使用class对象通过getDeclaredMethods方法获取到目标类中的方法集合
public void Test(){
User user = new User();
Class clazz = user.getClass();
Fiedld[] fields = clazz.getDeclaredFields();
for(Field dield : fields){
Class typeName = field.getType();//获取到属性的类型 string int double
String name = field.getName();
int i = field.getModifiers(); //属性的修饰符 1 public 0 default 2 private 4 protected
}
Method[] methods = clazz.getDeclaredMethods();
for(Method method : methods){
String name = method2.getName();
Class[] methodPara = method2.getParameterTypes();//参数类型 是个集合 可以继续遍历
Class returntype = method2.getReturnType();
}
}

反射技术获取到对象中的指定方法

标签:反射技术获取到对象中的指定方法

原文地址:http://blog.51cto.com/13654477/2088287

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!