利用java反射来实现输出对象的所有属性值

来源:互联网 发布:hadoop安装在windows 编辑:程序博客网 时间:2024/05/07 06:21

利用java反射来实现输出对象的所有属性值  

1.覆盖toString方法。 

2.在toString方法里面调用获取属性值的get方法。 
见源码: 
package org.lgzh.core.Action; 

import java.lang.reflect.Field; 
import java.lang.reflect.Method; 

public class BaseBean{ 
/** 

*/ 
private static final long serialVersionUID = 1L; 
public String toString(){ 
String s = ""; 
try { 
s = getPropertyString(this); 
} catch (Exception e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 

return s; 


public String getPropertyString(Object entityName) throws Exception { 
Class c = entityName.getClass(); 
Field field [] = c.getDeclaredFields(); 
StringBuffer sb = new StringBuffer(); 

sb.append("------ " + " begin ------\n"); 
for(Field f : field){ 
sb.append(f.getName()); 
sb.append(" : "); 
sb.append(invokeMethod(entityName,f.getName(),null)); 
sb.append("\n"); 

sb.append("------ " + " end ------\n"); 
return sb.toString(); 


public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception{
Class ownerClass = owner.getClass();

methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
Method method = null;
try {
method = ownerClass.getMethod("get" + methodName);
} catch (SecurityException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
return " can't find 'get" + methodName + "' method";
}

return method.invoke(owner);
}



使用方法: 
建一bean继承BaseBean 
public class UserVO extends BaseBean implements java.io.Serializable { 

// Fields 
/** 

*/ 
private static final long serialVersionUID = 1L; 
private Integer id; 
private String username; 
private String password; 
private Date regDate; 
private Integer visitTimes; 
private Date lastVisit; 
private Integer status; 

// Constructors 
/** default constructor */ 
public UserVO() { 

/** minimal constructor */ 
public UserVO(Integer id) { 
this.id = id; 


/** full constructor */ 
public UserVO(Integer id, String username, String password, Date regDate, Integer visitTimes, Date lastVisit, Integer status) { 
this.id = id; 
this.username = username; 
this.password = password; 
this.regDate = regDate; 
this.visitTimes = visitTimes; 
this.lastVisit = lastVisit; 
this.status = status; 


// Property accessors 
public Integer getId() { 
return this.id; 


public void setId(Integer id) { 
this.id = id; 

public String getUsername() { 
return this.username; 


public void setUsername(String username) { 
this.username = username; 

public String getPassword() { 
return this.password; 


public void setPassword(String password) { 
this.password = password; 

public Date getRegDate() { 
return this.regDate; 


public void setRegDate(Date regDate) { 
this.regDate = regDate; 

public Integer getVisitTimes() { 
return this.visitTimes; 


public void setVisitTimes(Integer visitTimes) { 
this.visitTimes = visitTimes; 

public Date getLastVisit() { 
return this.lastVisit; 


public void setLastVisit(Date lastVisit) { 
this.lastVisit = lastVisit; 

public Integer getStatus() { 
return this.status; 


public void setStatus(Integer status) { 
this.status = status; 




执行语句: 
UserVO userVO = new UserVO(); 
userVO.setId(1); 
userVO.setStatus(0); 
System.out.println(userVO); 
用System.out.println(userVO);打印对象即可输出里面对象
0 0