有没有办法遍历一个类中的所有数据成员?

junglesong 2005-04-20 01:27:09
这些类基本都是String类型成员变量+getter,setter函数的形式,有没有什么办法依次取得其中的每个成员变量呢?
...全文
172 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
junglesong 2005-04-20
  • 打赏
  • 举报
回复
输出结果如下:

,AFG,,,アフガニスタン,,,,0,,,,,
,AGO,,,アンゴラ,,,,0,,,,,
,ALB,,,アルバニア,,,,0,,,,,
,ANT,,,オランダリョウアンチル,,,,0,,,,,
,ARG,,,アルゼンチン,,,,0,,,,,
,ARM,,,アルメニア,,,,0,,,,,
,AUS,,,オ-ストラリア,,,,0,,,,,
,AUT,,,オ-ストリア,,,,0,,,,,
,AZE,,,アゼルバイジャン,,,,0,,,,,
,BEL,,,ベルギ-,,,,0,,,,,
,BGD,,,バングラデイシユ,,,,0,,,,,
junglesong 2005-04-20
  • 打赏
  • 举报
回复
重整一下再贴。

public static void MakeDateFile(String strFilename,String strClassName,Object[] objArr){
// Get Class
Class cls=null;

try {
cls = Class.forName(strClassName);
} catch (ClassNotFoundException e) {
}

// Trancate the Array
try{
Object obj,obj2;
obj=cls.newInstance();
int i,j;

Field[] fields = cls.getDeclaredFields();

// Print Fields
for(i=0;i<fields.length;i++){
System.out.print(fields[i].getName().toString()+",");
}
System.out.println();

// Print Varaiables
for(i=0;i<objArr.length;i++){
obj=objArr[i];

for(j=0;j<fields.length;j++){
Method method=cls.getMethod(getGetFunctionName(fields[j].getName().toString()),new Class [] { } );
obj2=method.invoke(obj,null);
System.out.print(obj2.toString()+",");
}
System.out.println();
}
}
catch(Exception e){
e.printStackTrace();
}
}

public static String getGetFunctionName(String strVariablename){
String strHead=strVariablename.substring(0,1);
String strTail=strVariablename.substring(1,strVariablename.length());

String strRetval="get"+strHead.toUpperCase()+strTail;

return strRetval;
}
junglesong 2005-04-20
  • 打赏
  • 举报
回复
用这个两个函数搞定了。

public static void MakeDateFile(String strFilename,String strClassName,Object[] objArr){
if(CommonUtil.isValidString(strFilename)==false){
CommonUtil.showErrorDialog("File name is invalid!");
return;
}

// Get Class
Class cls=null;

try {
cls = Class.forName(strClassName);
} catch (ClassNotFoundException e) {
}

// Trancate the Array
try{
Object obj,obj2;
obj=cls.newInstance();

Field[] fields = cls.getDeclaredFields();

for(int i=0;i<objArr.length;i++){
obj=objArr[i];

for(int j=0;j<fields.length;j++){
Method method=cls.getMethod(getGetFunctionName(fields[j].getName().toString()),new Class [] { } );
obj2=method.invoke(obj,null);
System.out.print(obj2.toString()+",");
}
System.out.println();
}
}
catch(Exception e){
e.printStackTrace();
}
}

public static String getGetFunctionName(String strVariablename){
String strHead=strVariablename.substring(0,1);
String strTail=strVariablename.substring(1,strVariablename.length());

String strRetval="get"+strHead.toUpperCase()+strTail;

return strRetval;
}
zcjl 2005-04-20
  • 打赏
  • 举报
回复
可以使用commons-beanutils.jar里面的org.apache.commons.beanutils.PropertyUtils
使用示例代码如下:

PropertyDescriptor[] descr = PropertyUtils.getPropertyDescriptors(bean);
Map props = new HashMap();
for (int i = 0; i < descr.length; i++) {
PropertyDescriptor d = descr[i];
if (d.getReadMethod() == null)
continue;
try {
props.put(d.getName(), PropertyUtils.getProperty(bean, d.getName()));
} catch (Exception e) {
}
}
junglesong 2005-04-20
  • 打赏
  • 举报
回复
大家提供一个针对具体实例的办法吧。
junglesong 2005-04-20
  • 打赏
  • 举报
回复
这些都是对类的操作阿,如何对应到类的实例中呢?
itjourney 2005-04-20
  • 打赏
  • 举报
回复
Class cls = Class.forName("xxx");
Field[] fields=cls.getFields();
MARS.nEIL 2005-04-20
  • 打赏
  • 举报
回复
通过Class的方法getDeclaredMethods,getDeclaredFields可以得到类中的方法和成员变量
jFresH_MaN 2005-04-20
  • 打赏
  • 举报
回复
Field [] f=Class.forName("xxx.yyy").getDeclaredMethods()
jFresH_MaN 2005-04-20
  • 打赏
  • 举报
回复
Field [] f=Class.forName("xxx.yyy").getDeclaredFields();

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧