11.29_ Common objects (equals () method code optimization for the object class). avi

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.