java - 在实际生产应用程序的典型模型中,toString() 函数的真正用途是什么?弹Java
问题描述
在 Java 中为用户创建典型模型时提到了一段代码,我不明白为什么要使用它。
例如,用户的模型会是这样的
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique= true, nullable = false)
private Long id;
private String firstName;
private String lastName;
private String email;
private String password;
private boolean enabled;
private String secret;
public User() {
super();
this.secret = Base32.random();
this.enabled = false;
}
// getters and setters
}
但是我在很多教程中发现的问题是我们应该另外添加:
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final User user = (User) obj;
if (!email.equals(user.email)) {
return false;
}
return true;
}
还有 toString 函数
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("User [id=").append(id).append(", firstName=").append(firstName).append(", lastName=").append(lastName).append(", email=").append(email).append(", password=").append(password).append(", enabled=").append(enabled).append("]");
return builder.toString();
}
为什么将此代码添加到模型中?背后的原因可能是什么?是强制性的吗?
谢谢您的帮助!
解决方案
toString
例如用于打印输出
没有toString
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Student s1=new Student(101,"Ram","Bengaluru");
Student s2=new Student(102,"Krishna","Chennai");
System.out.println(s1);
System.out.println(s2);
}
}
输出:
Student@7852e922
Student@4e25154f
在此示例中,s1
打印s2
位置,而不是字段值
和toString
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public String toString(){//overriding the toString() method
return rollno+" "+name+" "+city;
}
public static void main(String args[]){
Student s1=new Student(101,"Ram","Bengaluru");
Student s2=new Student(102,"Krishna","Chennai");
System.out.println(s1);
System.out.println(s2);
}
}
输出:
101 Ram Bengaluru
102 Krishna Chennai
在此示例中,它按预期打印了字段值。
对于equals
没有的方法equals
:
class Student{
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Student s1=new Student(101,"Ram","Bengaluru");
Student s2=new Student(101,"Ram","Bengaluru");
System.out.println(s1.equals(s2));
}
}
输出:假
在这个例子中,虽然 s1 和 s2 即
Student s1=new Student(101,"Ram","Bengaluru");
Student s2=new Student(101,"Ram","Bengaluru");
相同,仍然 s1.equals(s2) 的结果为假。
和equals
class Student {
int rollno;
String name;
String city;
Student(int rollno, String name, String city){
this.rollno=rollno;
this.name=name;
this.city=city;
}
public static void main(String args[]){
Student s1=new Student(101,"Ram","Bengaluru");
Student s2=new Student(101,"Ram","Bengaluru");
System.out.println(s1.equals(s2));
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (city == null) {
if (other.city != null)
return false;
} else if (!city.equals(other.city))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (rollno != other.rollno)
return false;
return true;
}
}
输出:真
在这个例子中,结果s1.equals(s2)
是正确的,即真