前置安装使用可看这个:/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 ;
}
public int getAge() {
return ;
}
public void setName(String name) {
= name;
}
public void setAge(int 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 ;
}
public int getAge() {
return ;
}
public void setName(String name) {
= name;
}
public void setAge(int 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 (!(this)) {
return false;
} else {
Object this$name = ();
Object other$name = ();
if (this$name == null) {
if (other$name == null) {
return () == ();
}
} else if (this$(other$name)) {
return () == ();
}
return false;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Student;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = ();
int result = result * 59 + ($name == null ? 43 : $());
result = result * 59 + ();
return result;
}
public String toString() {
return "Student(name=" + () + ", age=" + () + ")";
}
}
从以上编译文件可看出@Data和@Getter,@Setter的区别,@Data注解包含了@Getter@Setter,且有toString(),equals等方法