我是
靠谱客的博主
有魅力外套,最近开发中收集的这篇文章主要介绍
利用java反射来实现输出对象的所有属性值,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
利用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);打印对象即可
输出里面对象
值
最后
以上就是有魅力外套为你收集整理的利用java反射来实现输出对象的所有属性值的全部内容,希望文章能够帮你解决利用java反射来实现输出对象的所有属性值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复