Testing of override of toString()
程序员文章站
2022-04-08 16:32:32
...
At fisrt,thanks for the tips of Xia,I just run the situation he described.
When we want to print the properties of POJO, we can override the function toString() for conveninence , here is my testing code:
Created a object Student:
package mysrc;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Student implements Serializable{
int id;
String name;
//construct
Student (){
}
Student(int id,String name){
this.id = id;
this.name = name;
}
//override
public String toString(){
return ToStringBuilder.reflectionToString(this);
}
//the different way to implement this function
public String toStr(){
return new ToStringBuilder(this).append("id",id).append("name",name).toString();
}
}
Then test these two methods:
package mysrc;
public class ToStringTest {
public static void main(String args[]){
Student stu = new Student(10086,"tanglei");
System.out.println(stu.toString());
System.out.println(stu.toStr());
}
}
Then the result is :
[email protected][id=10086,name=tanglei]
[email protected][id=10086,name=tanglei]
Ok,that's all
推荐阅读
-
JavaSE的包装类,自动装箱和自动拆箱 ,字符窜转换,toString(),equals(), hashCode()的区别
-
Easy Integration Testing with Spring+Hibernate
-
PHP 魔术方法 __clone __toString(五),__clone__tostring
-
Easy Integration Testing with Spring+Hibernate
-
JavaScript中toString()方法的使用详解
-
Java——重写(Override)总结
-
Js自定义错误obj.prototype.toString代码实例讲解
-
XML笔记(Performance Testing)
-
OpenGL学习脚印:深度测试(depth testing)
-
ShaderLab: Culling & Depth Testing