谷歌的官方文档上看到重写的equals…

来源:互联网 发布:svm算法matlab实例 编辑:程序博客网 时间:2024/05/18 06:21
 @Override public boolean equals(Object o) {
     
// Return true if the objects are identical.
     
// (This is just an optimization, not required for correctness.)
     
if (this == o) {
       
return true;
     
}

     
// Return false if the other object has the wrong type.
     
// This type may be an interface depending on the interface's specification.
     
if (!(o instanceof MyType)) {
       
return false;
     
}

     
// Cast to the appropriate type.
     
// This will succeed because of the instanceof, and lets us access private fields.
     
MyType lhs = (MyType) o;

     
// Check each field. Primitive fields, reference fields, and nullable reference
     
// fields are all treated differently.
     
return primitiveField == lhs.primitiveField &&
             referenceField
.equals(lhs.referenceField) &&
             
(nullableField == null ? lhs.nullableField == null
                                   
: nullableField.equals(lhs.nullableField));
   
}
原创粉丝点击