Package cn.itcast_03;
public class Student {private String name;private int age;
Public Student () {Super ();}
Public Student (String name, int.) {Super ();THIS.name = name;This.age = age;}
Public String GetName () {return name;}
public void SetName (String name) {THIS.name = name;}
public int getage () {return age;}
public void Setage (int.) {This.age = age;}
@Overridepublic 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 (age! = other.age)return false;if (name = = null) {if (other.name! = null)return false;} else if (!name.equals (other.name))return false;return true;}//@Override//public boolean equals (Object obj) {//Return true;//Here to improve, depending on the member variables here to determine whether to return TRUE or false//The only thing that's going to compare here is name and age//.However, name is a string type, and string is a reference type, so you cannot use the = = comparison in this case, you should compare it with equals (using the Equals () method inside the string class, and compare the value content, not the address)//The Equals () method of the string is overridden from the object class, comparing whether the contents of the string are the same//This--s1//Obj--s2//We're going to use a unique member variable for the student class, so we're going down.Student s = (Student) obj; S---obj--s2;//if (This.name.equals (s.name) && this.age = = s.age) {//Return true;//}else {//Return false;//}//}//@Override//public boolean equals (Object obj) {//To improve efficiency//if (this = = obj) {//Return true;//}////In order to provide the robustness of the program//I'll start by judging if obj is not an object of the student, and if it is, then do a downward transformation, if not, and return false directly. //At this point, we want to determine whether the object is an object of a class?//Remember a format: Object name instanceof class name//Indicates whether the object name is an object of the class name//if (! ( obj instanceof Student)) {//Return false;//}//If it is, continue////Student s = (Student) obj;//System.out.println ("The same object, still need to be transformed and compared?"); /Return This.name.equals (s.name) && this.age = = s.age;//}}
From for notes (Wiz)
11.29_ Common objects (equals () method code optimization for the object class). avi