Why do you need to overload Equals?

xiaoxiao2021-03-06  41

The benefits of overloading Equals are required in the physical class. (Java)

The base class Object in Java already has an equals method, the prototype is

Public Boolean Equals (Object Obj) {Return (this == Obj);

Obviously, the comparison is the same as the object pointer, that is, the internal value of the two entities is the same, but the memory location is not the same, but the default method of Object is impossible, that is, Equals. The call will return false. The ideas and solutions generated below I have encountered one of the problems encountered.

The system is a typical three-layer structure .WebStart technology is a front -weblogic application server -Oracle database. We write programs are clearly divided into two parts: interface - business logic. Key points are, between the two Category: RequestEvent and ResponseEvent.event encapsulated data we need to transfer, we use the case in units, and the data corresponding to each use case can be encapsulated into VO, we become a value object (Value, Object).

VO is a simple data package we define, attribute, and Getter and Setter methods for attributes. Implement VO: public interface ivueObject Extends serializable {} public Abstract Class BasevalueObject Implements IValueObject {

} For example, I have to encapsulate a bank information, as the interaction of the former and back, information, banking, ICBC, China Merchants Bank ...), bank code, bank name, etc. (We assume these three information) VO is defined as public class YhxxVO extends BaseValueObject {private String yhhbdm; private String yhdm; private String yhmc; // getter public String getYhhbdm () {return yhhbdm;} ... // setter public void setYhhbdm (String yhhbdm) {this. Yhhbdm = yhbdm;} ...}

Ok, we pass the RequestEventEvent, all bank information in Beijing. Transfer to the background, the background acceptance, query the database, get a list of bank information, in the returned ResponseEvent, we get a list of bank information YHXLIST, type For ArrayList. Since there are other large fields or other reasons due to the data sheets, there may be repetitive information. When you need to handle things, you can filter your duplicate information in YHXXLIST and put it in a list called Result. The probably code will be like this: (assuming that there is no NULL null value to handle)

ArrayList Result = new arraylist (); for (int i = 0, size = yhxxlist.size (); i

This is a two cycle, and the YHXXVO variable is defined twice, and the IF of the internal cycle is really long, so it seriously affects readability, so that people who have just taken the code are a bit of not easy, such as someone It will be proposed to be empty, the inner cycle is to wait. The programmer familiar with ArrayList may immediately think of it, use the ArrayList's Contain method, do not solve this indifferent code, yes, it is right. According to This idea, we can have this code:

ArrayList Result = new arraylist (); for (int i = 0, size = yhxxlist.size (); i

It's too clear, it is very clear that the readability of these two codes is abnormal.

However, things are often not so simple. After running this session, you will find that RESULT and YHXXLIST are exactly the same, and the filter repeated information is filtered out, why? Because, the contain function is in an existing object Look for whether there is the same number of objects, the pseudo code of the judgment process is probably:

Cycle {IF in all existing objects (current object. Equals) {Return true;}} return false;

Note that he calls Equals, and our VO does not overload Equals, so inherits the processing method of object: Compare pointers! Therefore, the VO we have is not likely to be the same. The processing method also became obvious - heavy daquals .

However, is it to write an equals for our VO, obviously this is not a smart method, the so-called strategies, it is to modify the Equals method to BasevalueObject, all his subclasses can inherit his equals method, Instead of Object's original method.

The difficulty is that our VO's judgment is equal, but it does not know what is the value inside the future subclass. Java is very wonderful features: reflective mechanism. Below is the basevalueObject after we deal with

Public Abstract Class BasevalueObject Implements IValueObject {Public Boolean Equals (Object Obj) {IF (OBJ == NULL) {// Parameter is empty Return False;}

IF (this == obj) {// and compare Return true;} if (! Obj.getClass (). Equals (getClass ())) {// Introduction is not the same type Return False;}

Method [] Methods = this.getClass (). GetMethods (); // Get all public methods Field [] fields = this.getClass (). Getfields (); // acquisition all public property Boolean flag = true; // logo Whether the object is equal to Try {for (INT i = 0; Flag && i 0) {Continue;} String Tmp = methodname.trim (). Substring (3); Flag = (Methods [i ] .invoke (this, null)). Equals (Methods [i] .invoke (obj, null)); // Contrast to obtain the attribute value of the two objects, etc.}} for (int i = 0; Flag && i

Our needs reach, the simple code can work well because Contain can accurately identify the same bank information.

Why do we need to compare public properties and public methods, depending on the agreement, if the two value objects are the same attribute value, we think they are equally, this application is only limited to this convention, that is, we pass our outside world. The attribute that can be observed is to determine whether the two are equal. If you have a private domain that is not known, and this private domain just becomes the sign of your identification, this reflex mechanism is unhealthy, but carefully thinking about it. Think, such applications are rare.

Ok, if you have the same application and needs, I hope that these words can help you! Of course, any applications and problems have specific areas, there are specific analysis, but the method itself is also very beneficial.

Cao Xihua 2005/01/03 2005-1-3 15:25:51

转载请注明原文地址:https://www.9cbs.com/read-73702.html

New Post(0)