java 循环把对象属性 输出
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 循环把对象属性 输出相关的知识,希望对你有一定的参考价值。
要把 对象属性的值 循环 输出 ,不要一个一个属性 输出,对象名.属性 ...属性 太多,有办法 一次 输出 全部属性么
写成这样了...
import java.lang.reflect.Field;
Class cla = gooddetail.getClass();
Field[] field = cla.getDeclaredFields();
for(Object str : field)
System.out.println(str.toString());
package com.yao.util.bean;
import java.lang.reflect.Method;
import com.yao.util.string.StringUtil;
public class BeanUtil
/**
* 打印对象
*
* @param o
*/
public static String toObjectString(Object o)
return toObjectString(o, 1);
private static String toObjectString(Object o, int dep)
if (null == o) return null;
StringBuffer c = new StringBuffer();
Class<?> clazz = o.getClass();
Method[] methods = clazz.getMethods();
c.append(o.getClass().getSimpleName()).append(" ").append("\n");
for (Method method : methods)
String mname = method.getName();
Class<?> type = method.getReturnType();
if (mname.length() <= 3 || mname.equals("getClass"))
continue;
if (mname.substring(0, 3).equals("get"))
try
Object returnO = method.invoke(o, new Object[] );
mname = mname.replaceFirst("get", "");
mname = StringUtil.toLowerCase(mname.charAt(0) + "") + mname.substring(1);
c.append(StringUtil.repeat(" ", dep)).append(type.getSimpleName()).append(" ").append(mname);
if (null == type.getPackage() || type.getPackage().equals(Package.getPackage("java.lang")))
c.append(" = ").append(StringUtil.toCodeString(returnO));
else
c.append(" = ").append(toObjectString(returnO, dep + 1));
c.append(";").append("\n");
catch (Exception e)
// e.printStackTrace();
c.append(StringUtil.repeat(" ", dep - 1)).append("");
return c.toString();
public static void main(String[] args) throws SecurityException, NoSuchMethodException
TestBean test = new TestBean();
test.setA(54);
test.setB("asdf");
test.setC(587483L);
System.out.println(BeanUtil.toObjectString(test));
public static class TestBean
private int a;
private String b;
private Long c;
public int getA()
return a;
public void setA(int a)
this.a = a;
public String getB()
return b;
public void setB(String b)
this.b = b;
public Long getC()
return c;
public void setC(Long c)
this.c = c;
@Override
public String toString()
String str = a + " | " + b + " | " + c;
return str;
参考技术B // 你用这个类吧,我写的,平常调试用的
package com.yao.util.bean;
import java.lang.reflect.Method;
import com.yao.util.string.StringUtil;
public class BeanUtil
/**
* 打印对象
*
* @param o
*/
public static String toObjectString(Object o)
return toObjectString(o, 1);
private static String toObjectString(Object o, int dep)
if (null == o) return null;
StringBuffer c = new StringBuffer();
Class<?> clazz = o.getClass();
Method[] methods = clazz.getMethods();
c.append(o.getClass().getSimpleName()).append(" ").append("\n");
for (Method method : methods)
String mname = method.getName();
Class<?> type = method.getReturnType();
if (mname.length() <= 3 || mname.equals("getClass"))
continue;
if (mname.substring(0, 3).equals("get"))
try
Object returnO = method.invoke(o, new Object[] );
mname = mname.replaceFirst("get", "");
mname = StringUtil.toLowerCase(mname.charAt(0) + "") + mname.substring(1);
c.append(StringUtil.repeat(" ", dep)).append(type.getSimpleName()).append(" ").append(mname);
if (null == type.getPackage() || type.getPackage().equals(Package.getPackage("java.lang")))
c.append(" = ").append(StringUtil.toCodeString(returnO));
else
c.append(" = ").append(toObjectString(returnO, dep + 1));
c.append(";").append("\n");
catch (Exception e)
// e.printStackTrace();
c.append(StringUtil.repeat(" ", dep - 1)).append("");
return c.toString();
public static void main(String[] args) throws SecurityException, NoSuchMethodException
TestBean test = new TestBean();
test.setA(54);
test.setB("asdf");
test.setC(587483L);
System.out.println(BeanUtil.toObjectString(test));
public static class TestBean
private int a;
private String b;
private Long c;
public int getA()
return a;
public void setA(int a)
this.a = a;
public String getB()
return b;
public void setB(String b)
this.b = b;
public Long getC()
return c;
public void setC(Long c)
this.c = c;
@Override
public String toString()
String str = a + " | " + b + " | " + c;
return str;
参考技术C 使用Object下的getclass()反射机制,具体介绍随便java书上都有吧。 参考技术D 用反射啊本回答被提问者采纳
JAVA循环获取值
已知某实体类属性分别以英文数字one、two、three、...ten命名,提供了set、get的方法,求如何通过循环分别获取getOne()、getTwo()、getThree()、...getTen()的值
for(int i=1;i<=10;i++)
test.get(这里填One、Two、Three、...Ten)()
List countList=new ArrayList();
countList.add(one);
...
countList.add(ten);
for(int i=0;i<countList.size();i++)
test = countList.get(i);
追问
能不能通过
String num="";
switch
case 1:
num="One";
break;
........
test.get+num() 这里无法处理
不可以的。你天真了。
java中的getter和setter是预先生成好的,没有你这么用的。
代码如下:
public class User
private int age;
private String name;
private int sex;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getSex()
return sex;
public void setSex(int sex)
this.sex = sex;
User(int age,int sex,String name)
this.age=age;
this.sex=sex;
this.name=name;
public static void main(String[] args)
User u=new User(20, 1, "test");
Class c=u.getClass();
Method[] ms=c.getDeclaredMethods();
for(Method m:ms)
String name=m.getName();
if(name.startsWith("get"))
try
Object r=m.invoke(u, null);
System.out.println(r);
catch (IllegalAccessException e)
e.printStackTrace();
catch (IllegalArgumentException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();
以上是关于java 循环把对象属性 输出的主要内容,如果未能解决你的问题,请参考以下文章