Third code Model: object Comparison
Before explaining the specific concepts, look at a form of reference passing that this class receives for this class of objects.
Example: Watcher code (don't think about the meaning of code for the time being)
Class Person { private String name; Public person (String name) { THIS.name = name; }//Receive this class of object public void change (person temp) { temp.name = " John Doe " ; directly through the object . Property "Call } Public String GetName () { return this.name; } } public class Testdemo { public static void Main (String args[]) { Person per = new Person ("Zhang San"); System.out.println (Per.getname ()); Per.change (per); receive this class, do not care about meaning, only care about grammar System.out.println (Per.getname ()); } } |
Under normal circumstances, if the private package is used, the external cannot be manipulated by the "object. Properties" form, but if the object is now passed back to the class.
So if there are now two Person class objects (name, age property), what do you do if you want to compare two objects for equality? Each property is compared separately, and if it is all the same, it represents equality, and now comes the following piece of code.
Class Person { private String name; private int age; Public person () {} Public person (String Name,int age) { THIS.name = name; This.age = age; } Public String GetName () { return this.name; } public int getage () { return this.age; } } public class Testdemo { public static void Main (String args[]) { Person PerA = new Person ("Zhang San", 20); Person Perb = new Person ("Zhang San", 20); if (Pera.getname (). Equals (Perb.getname ()) && Pera.getage () = = Perb.getage ()) { System.out.println ("Two objects are equal. ") ; } else { System.out.println ("Two objects are not equal. ") ; } } } |
The object comparison operation has been successful at this point, but the code is problematic.
At this point the code all the validation function to the main method to complete, in fact, the main method is a client, in the main method should not be involved in the excessive business logic, only need to do simple operation. In addition, the process of information comparison should be the function that each object has. Should be defined within the class.
Class Person { private String name; private int age; Public person () {} Public person (String Name,int age) { THIS.name = name; This.age = age; } Public String GetName () { return this.name; } public int getage () { return this.age; } Tentative this method name is compare () // This method will have two objects: the current object This , the incoming Person Public boolean compare (person person) { if (this = person) {// address same return true; } if (person = = null) { return false; } // Objects can be used directly when they are passed back to the class . Properties "Access if (This.name.equals (person.name) && this.age = = person.age) { return true; } return false; } } public class Testdemo { public static void Main (String args[]) { Person PerA = new Person ("Zhang San", 20); Person Perb = new Person ("Zhang San", 20); if (Pera.compare (null)) { System.out.println ("Two objects are equal. ") ; } else { System.out.println ("Two objects are not equal. ") ; } } } |
From the standard, the simple Java class is required to provide object comparison operations, go back to write a few.
Java Learning-a third code model