废话少说,直接运行看^!^ 看不懂你抽死我~,~
Commons-lang-2.5 实体对象toString方法重写
这个重写实体toString()到底干啥用呢,其实就是为了方便打印.如果所有人都遵循commons-lang重写toString(),那开发效率将大大的提高,维护起来也都按统一的标准来的,代码可读性强.
下面的代码中,我给出了2种重写toString()的方法,与大家共享.
ToStringStyle这个类是指定输出样式的类
ToStringStyle.DEFAULT_STYLE
test.common.db.Var@a62fc3[id=phl,value_t=100.0,Date_Time=1985-12-26 23:20:00]
ToStringStyle.MULTI_LINE_STYLE
test.common.db.Var@a62fc3[
id=phl
value_t=100.0
Date_Time=1985-12-26 23:20:00
]
ToStringStyle.NO_FIELD_NAMES_STYLE
com.discursive.jccook.lang.builders.PoliticalCandidate@icd2e5f[Jefferson,Thomas]
test.common.db.Var@a62fc3[phl,100.0,1985-12-26 23:20:00]
ToStringStyle.SIMPLE_STYLE
phl,100.0,1985-12-26 23:20:00
package test.common.lang;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
public class util {
public static void main(String[] args) {
int[] intArray = new int[] { 1, 2, 3, 5, 6, 7, 8, 9 };
int[][] multiDimension = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
System.out.println("intArray: " + ArrayUtils.toString(intArray));
System.out.println("nultiDimension: " + ArrayUtils.toString(multiDimension));
// 如果数组为空
String[] strs = null;
System.out.println("Strings: " + ArrayUtils.toString(strs, "unknow"));
// 打印collection
List list = new ArrayList();
list.add("Foo");
list.add("Blah");
System.out.println(ArrayUtils.toString(list.toArray()));
// 倒序数组
ArrayUtils.reverse(intArray);
System.out.println(ArrayUtils.toString(intArray));
}
}
/*
运行结果:
intArray: {1,2,3,5,6,7,8,9}
nultiDimension: {{1,2,3},{4,5,6},{7,8,9}}
Strings: unknow
{Foo,Blah}
{9,8,7,6,5,3,2,1}
*/
Commons-lang-2.5 实体对象toString方法重写
这个重写实体toString()到底干啥用呢,其实就是为了方便打印.如果所有人都遵循commons-lang重写toString(),那开发效率将大大的提高,维护起来也都按统一的标准来的,代码可读性强.
下面的代码中,我给出了2种重写toString()的方法,与大家共享.
ToStringStyle这个类是指定输出样式的类
ToStringStyle.DEFAULT_STYLE
test.common.db.Var@a62fc3[id=phl,value_t=100.0,Date_Time=1985-12-26 23:20:00]
ToStringStyle.MULTI_LINE_STYLE
test.common.db.Var@a62fc3[
id=phl
value_t=100.0
Date_Time=1985-12-26 23:20:00
]
ToStringStyle.NO_FIELD_NAMES_STYLE
com.discursive.jccook.lang.builders.PoliticalCandidate@icd2e5f[Jefferson,Thomas]
test.common.db.Var@a62fc3[phl,100.0,1985-12-26 23:20:00]
ToStringStyle.SIMPLE_STYLE
phl,100.0,1985-12-26 23:20:00
package test.common.db;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class Var {
private String id;
private float value_t;
private String Date_Time;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public float getValue_t() {
return value_t;
}
public void setValue_t(float valueT) {
value_t = valueT;
}
public String getDate_Time() {
return Date_Time;
}
public void setDate_Time(String dateTime) {
Date_Time = dateTime;
}
//简单封装
// @Override
// public String toString() {
// return ReflectionToStringBuilder.toString(this,ToStringStyle.MULTI_LINE_STYLE);
//}
// 自定义封装
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", id).append("value_t", value_t).append("Date_Time", Date_Time).toString();
}
}
package test;
import test.common.db.Var;
public class Test {
public static void main(String[] args) throws Exception {
//ApplicationContext factory = new ClassPathXmlApplicationContext("beans.xml");
//DataSource ds = (DataSource) factory.getBean("dataSource");
//System.out.println(ds.getConnection());
//?useUnicode=true&characterEncoding=utf-8
Var var = new Var();
var.setId("phl");
var.setValue_t(100);
var.setDate_Time("1985-12-26 23:20:00");
System.out.println(var.toString());
}
}
/*
运行结果:
test.common.db.Var@a62fc3[
id=phl
value_t=100.0
Date_Time=1985-12-26 23:20:00
]
*/