java重写tostring_Java实体类如何快速重写toString()
当我们写⼀个实体类的时候,我们有时需要重写toString⽅法以⽅便我们查看类中字段的值,如果在重写toString()时,纯⼿写各个字段的话(如下⽅代码),字段少了还好,字段多的话就....emmm!
java的tostring方法static class Model extends Object {
String name;
int age;
String birthday;
public String getName() {
return name;
}
public Model setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Model setAge(int age) {
this.age = age;
return this;
}
public String getBirthday() {
return birthday;
}
public Model setBirthday(String birthday) {
this.birthday = birthday;
return this;
}
@NonNull
@Override
public String toString() {
return "[name=" + name
+ ",age=" + age
+ ",birthday=" + birthday+"]";
}
}
所以有必要寻求⼀种快速的⽅法,其实很简单:
static class Model extends Object {
String name;
int age;
String birthday;
public String getName() {
return name;
}
public Model setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Model setAge(int age) {
this.age = age;
return this;
}
public String getBirthday() {
return birthday;
}
public Model setBirthday(String birthday) {
this.birthday = birthday;
return this;
}
@NonNull
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
for (Field declaredField : DeclaredFields()) { try {
result
.Name())
.append("=")
.(Model.this))
.append(",");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return result.substring(0, result.length() - 1) + "]";
// return "[name=" + name
/
/ + ",age=" + age
// + ",birthday=" + birthday + "]";
}
}
其实就是简单⽤到了Class中的⼏个⽅法。
那我们不能每新建⼀个实体类重写toString()时都写这些代码吧,虽然不多,但是我懒啊,那怎么办呢?很好办,把逻辑抽出来,新建⼀个静态⽅法,⽤的时候直接调⽤就好了。
static class Model extends Object {
String name;
int age;
String birthday;
public String getName() {
return name;
}
public Model setName(String name) {
this.name = name;
return this;
}
public int getAge() {
return age;
}
public Model setAge(int age) {
this.age = age;
return this;
}
public String getBirthday() {
return birthday;
}
public Model setBirthday(String birthday) {
this.birthday = birthday;
return this;
}
@NonNull
@Override
public String toString() {
return modelToString(this);
// StringBuilder result = new StringBuilder("[");
//
// for (Field declaredField : getClass().getDeclaredFields()) { // try {
// result
// .Name())
// .append("=")
// .(Model.this))
// .append(",");
// } catch (IllegalAccessException e) {
/
/ e.printStackTrace();
// }
// }
//
// return result.substring(0, result.length() - 1) + "]";
// return "[name=" + name
// + ",age=" + age
// + ",birthday=" + birthday + "]";
}
}
public static String modelToString(T t) {
StringBuilder result = new StringBuilder("[");
for (Field declaredField : t.getClass().getDeclaredFields()) {
try {
result
.Name())
.append("=")
.(t))
.append(",");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return result.substring(0, result.length() - 1) + "]";
}
这样就简单多了,说了那么多我们简单测试⼀下。
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
System.out.println(new Model()
.setBirthday("2018-02-02")
.setName("娃娃")
.setAge(2)
.toString());
long sum = System.currentTimeMillis() - start;
System.out.println(String.valueOf(sum / 1000) + " 秒\n" + sum + " 毫秒");
}
输出如下:
[name=娃娃,age=2,birthday=2018-02-02]
0 秒
3 毫秒
Process finished with exit code
那种样是不是就完⼯了呢?其实不是,如果你访问的是没有访问权限的字段它会报错,就是这段try {
result
.Name())

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。