Java学习笔记-@Data和@Getter,@Setter区别

前置安装使用可看这个:https://blog.csdn.net/mumuwang1234/article/details/110707355

 

使用@Getter和@Setter

代码如下:

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

 

编译后如下:

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

 

使用@Data:

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

编译后的文件如下:

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

 

从以上编译文件可看出@Data和@Getter,@Setter的区别,@Data注解包含了@Getter@Setter,且有toString(),equals等方法

你可能感兴趣的:(java)