Notas del estudio de Java: la diferencia entre @Data y @Getter, @Setter

Para la preinstalación y el uso, consulte esto: https://blog.csdn.net/mumuwang1234/article/details/110707355

 

Utilice @Getter y @Setter

el código se muestra a continuación:

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

 

Después de la compilación es la siguiente:

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;
    }
}

 

Utilice @Data:

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

El archivo compilado es el siguiente:

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() + ")";
    }
}

 

En el archivo compilado anterior, podemos ver la diferencia entre @Data, @Getter y @Setter. La anotación @Data incluye @ Getter @ Setter, y hay métodos como toString (), equals, etc.

Supongo que te gusta

Origin blog.csdn.net/mumuwang1234/article/details/112134486
Recomendado
Clasificación