Java study notes-the difference between @Data and @Getter, @Setter

For pre-installation and use, please see this: https://blog.csdn.net/mumuwang1234/article/details/110707355

 

Use @Getter and @Setter

code show as below:

@Getter
@Setter
public class Student {
   public String name;
   public int age;
}

 

After compilation is as follows:

public class Student {
    public String name;
    public int age;

    public Student() {
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

Use @Data:

@Data
public class Student {
   public String name;
   public int age;
}

The compiled file is as follows:

public class Student {
    public String name;
    public int age;

    public Student() {
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Student)) {
            return false;
        } else {
            Student other = (Student)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name == null) {
                        return this.getAge() == other.getAge();
                    }
                } else if (this$name.equals(other$name)) {
                    return this.getAge() == other.getAge();
                }

                return false;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof Student;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        result = result * 59 + this.getAge();
        return result;
    }

    public String toString() {
        return "Student(name=" + this.getName() + ", age=" + this.getAge() + ")";
    }
}

 

From the above compiled file, we can see the difference between @Data, @Getter and @Setter. The @Data annotation includes @Getter@Setter, and there are methods such as toString(), equals, etc.

Guess you like

Origin blog.csdn.net/mumuwang1234/article/details/112134486