java 反射 属性顺序_java反射得到的方法数组的顺序
展開全部
看了下你好像需要set和get方法,如果你知道屬性的名字的話不需要遍歷可以這樣獲取,這個是我以前的demo的一個32313133353236313431303231363533e58685e5aeb931333332633561片段:Class?clazz=Class.forName("com.demo.reflectdemo.Student");
Object?obj=clazz.newInstance();
Field?fields[]=clazz.getDeclaredFields();
for(int?i=0;i
{
PropertyDescriptor?pd=new?PropertyDescriptor(fields[i].getName(),?clazz);
Method?method=pd.getWriteMethod();
Class?type=pd.getPropertyType();
System.out.println(type.getName());
if(type.equals(int.class)){
int?id=1;
method.invoke(obj,?id);
}
else?if(type.equals(String.class)){
method.invoke(obj,"張三");
}
}
主要是這里
PropertyDescriptor pd=new PropertyDescriptor(fields[i].getName(), clazz);
Method method=pd.getWriteMethod();
這樣就能獲取get方法了,再調用invoke就行了
總結
以上是生活随笔為你收集整理的java 反射 属性顺序_java反射得到的方法数组的顺序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信网页版电脑版下载(微信电脑版官网下载
- 下一篇: java字符串构造函数的应用_Strin