简体   繁体   English

我应该总是覆盖 equals、hashcode 和 toString 方法吗?

[英]Should I always override equals, hashcode and toString methods?

I have rather simple question: According to best programming practices, when I create a new model class should I always override equals, hashcode and toString methods?我有一个相当简单的问题:根据最佳编程实践,当我创建一个新的模型类时,我应该总是覆盖 equals、hashcode 和 toString 方法吗? Even I do not intend to compare objects of specific class?即使我不打算比较特定类的对象?

Each of these methods has its own significance.这些方法中的每一种都有其自身的意义。 equals and hashCode methods are used for comparison and hashing and toString mainly for logging purposes. equalshashCode方法用于比较和散列,而toString主要用于记录目的。 If you don't want any of these functionalities, you are not required to implement them.如果您不想要任何这些功能,则不需要实现它们。

Another best practice is to not implement code that is never used.另一个最佳实践是不要实现从未使用过的代码。 So implement those methods mostly on demand.所以主要是按需实现这些方法。 One reason to implement those methods without using them right away would be to assure a specific implementation or behaviour which one other developer maybe could not be aware of when he implements it on demand.在不立即使用这些方法的情况下实现这些方法的一个原因是确保其他开发人员在按需实现时可能无法意识到的特定实现或行为。 So for your question: No, not always.所以对于您的问题:不,并非总是如此。

Even though above answers are correct, I would like to add on why these methods are actually overridden.尽管上述答案是正确的,但我想补充一下为什么这些方法实际上被覆盖了。 If your model class will be used as a key of a Map or a Tree , or in a Set , its best practice to override equals and hashCode method (as it will be needed for comparing 2 model class objects).如果您的模型类将用作MapTreeSet 的键,则最佳实践是覆盖equalshashCode方法(因为比较 2 个模型类对象将需要它)。

toString you will need to override, if you want you want to display your model class data in a efficient way.如果您希望以有效的方式显示模型类数据,则需要覆盖toString Suppose if you have a model class, Dummy , having 2 fields, field1 and field2 .假设您有一个模型类Dummy ,它有 2 个字段field1field2

// Without overriding toString
System.out.println("field1: " + dummyObj.field1 + " field2"+dummyObj.field2);

// With overriding toString
System.out.println(dummyObj);

// Your overridden toString
public String toString(){
  return "field1: " + this.field1 + " field2"+this.field2;
}

As per my own perspective按照my own perspective

You should not override always hashcode and equal but you should override toString你不应该总是覆盖hashcodeequal但你应该覆盖toString

why toString?

say you have a class with more than 10 properties and you are debugging an object that it is populated or not,假设您有一个具有 10 个以上属性的class ,并且您正在调试一个是否已填充的对象,

I would prefer to not check through getter instead print out an object will reduce my time我宁愿不检查 getter 而是打印出一个对象会减少我的时间

hashcode and equal?

these two methods come into the picture whenever we use hash-based collection like:每当我们使用基于哈希的集合时,就会出现这两种方法,例如:

(1) hashSet, (2) hashTable, (3) hashMap and few more

if you are not intended to use collection and comparing of an object!如果您不打算使用对象的收集和比较! then you just have useless code which creates ambiguity那么你只有无用的代码,这会产生歧义

The decision should be based on该决定应基于

  • consistency (guide lines, and code checkers)一致性(指南和代码检查器)
  • quality质量
  • code style代码风格
  • effort (writing, reading & understanding implications)努力(写作、阅读和理解含义)

I myself am rather lax, feeling that not every class needs that much of attention (for a possible future use).我自己比较松懈,感觉不是每个班级都需要那么多关注(为了将来可能使用)。

Though guide lines should be followed stoically.虽然应该坚忍地遵循指导方针。 Code checkers almost never be ignored.代码检查器几乎永远不会被忽视。

As IDEs and even libraries provide easily created boiler code for generating the methods, the effort is low, but the code style can become unnecesarily bloated.由于 IDE 甚至库提供了用于生成方法的轻松创建的锅炉代码,因此工作量很小,但代码样式可能会变得不必要地臃肿。 Especially for inner classes.特别是对于内部类。

And of course "callback" event handling classes do not need them.当然,“回调”事件处理类不需要它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM