Phase 1 Language Basics + equals method High _1-3-Java language senior _03- common API _ the second part of Section 1 of the Object class _3_ override the Object class


Now rewrite equlas Object methods. Comparison of the property values of two objects. But the problem is multi-state


solution is downcast
,



the same name and age, the comparison is true


there is a problem, if I pass a parameter ArrayList. It is also a subclass of Object. So strong parameters into the class Person, then it will error.

ArrayList strong turn Person will give an error.



Increase judgment, instanceof to determine the type

so that if it passed directly ArrayList returns false, and no exception is thrown.


Passed a null value in the past. Default is null reference type

increases if judgment, if it is null the direct return false


if the comparing p1 and p1
increases a determination, if it is passed this parameter obj itself. Direct return true, improve the efficiency of the program

pass their own direct return true

,
 

Adding shortcuts






Alt + Insert

Select Next


next--

which is generated code





below to understand Objects.equals


 

Guess you like

Origin www.cnblogs.com/wangjunwei/p/11226258.html