在实际生产应用程序的典型模型中,toString() 函数的真正用途是什么?跳Java [英] what is the real use of a toString() function in a typical model in a real production app? sping Java

查看:18
本文介绍了在实际生产应用程序的典型模型中,toString() 函数的真正用途是什么?跳Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用 Java 为用户创建典型模型时提到了一段代码,我不明白为什么要使用它.

There is a code mentioned with creating a typical model for User in Java that I don't understand why it is used.

例如一个用户模型就是这样

for example a model for a User would be like that

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

但是我在很多教程中发现的问题,我们应该另外添加这个:

But the issue that I find in a lot of tutorials that we should add this in addition:

@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 函数

And the toString function too

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

为什么将这段代码添加到模型中?背后的原因可能是什么?是强制性的吗?

Why this code is added to the model? What could be the reason behind it? is it mandatory?

感谢您的帮助!

推荐答案

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

在这个例子中,s1s2 打印了位置,而不是字段值

In this example, s1 and s2 printed the location, not the field values

使用 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

在这个例子中,它按预期打印了字段值.

In this example, it printed the field values as expected.

对于 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 即

In this example, though s1 and s2 i.e.

   Student s1=new Student(101,"Ram","Bengaluru");  
   Student s2=new Student(101,"Ram","Bengaluru");  

一样,s1.equals(s2)的结果仍然是false.

are same, still the result of s1.equals(s2) is false.

等于

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) 的结果是正确的,即 true

In this example, result of s1.equals(s2) is correct i.e. true

这篇关于在实际生产应用程序的典型模型中,toString() 函数的真正用途是什么?跳Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆