Lombok是一个很好的工具,节省了很多重写方法,而@ToString就是节省了ToString方法,lombok中@ToString就是节省了我们在模型中的冗余代码下面就来举个例子
- import java.util.Arrays;
- public class ToStringExample {
- private static final int STATIC_VAR = 10;
- private String name;
- private Shape shape = new Square(5, 10);
- private String[] tags;
- private int id;
- public String getName() {
- return this.getName();
- }
- public static class Square extends Shape {
- private final int width, height;
- public Square(int width, int height) {
- this.width = width;
- this.height = height;
- }
- @Override
- public String toString() {
- return "Square(super=" + super.toString() + ", width=" + this.width + ", height=" + this.height + ")";
- }
- }
- @Override
- public String toString() {
- return "ToStringExample(" + this.getName() + ", " + this.shape + ", " + Arrays.deepToString(this.tags) + ")";
- }
- }
引入@ToString注解后的代码
- @ToString(callSuper=true, includeFieldNames=true)
- public static class Square extends Shape {
- private final int width, height;
- public Square(int width, int height) {
- this.width = width;
- this.height = height;
- }
- }
- }