File tree Expand file tree Collapse file tree
Java_SE_Base/commonClasses/Object Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -7,9 +7,7 @@ Java把现实中的任何事物都当做一个对象(Object), Java是面向对
77``` java
88public class Dog extends Object {
99}
10-
1110与
12-
1311public class Dog {
1412}
1513```
@@ -49,6 +47,7 @@ public class Object {
4947 }
5048
5149 /* 用于对象的复制,本地方法。
50+ * clone函数返回的是一个引用,指向的是新的clone出来的对象,此对象与原对象分别占用不同的堆空间。
5251 * 以前这个函数不是本地方法,需要实现Cloneable接口。覆写时
5352 * 还要改为public的权限。
5453 */
@@ -104,3 +103,28 @@ public class Object {
104103}
105104```
106105
106+ ### 覆写equals方法和hashCode方法
107+
108+ 在覆写equals方法时应该遵循以下几点:
109+
110+ - 自反性:任何非空引用x : x.equals(x) 应该返回true。
111+ - 对称性:任何非空引用x,y :x.equals(y) 和 y.equals(x) 返回结果应该一致。
112+ - 传递性:任意非空引用x,y :x.equals(y) <==> y.equals(z) <==> z.equals(x)。
113+ - 一致性:任意非空引用x,y :x.equals(y),多次执行,结果应该一致。
114+ - 约定 :任意非空引用x :x.equals(null) ,应该返回false。
115+ - 覆写equals方法时,应该同时覆写hashCode方法,如果两个对象被equals()方法判断为相等,那么它们就应该拥有同样的hash code。
116+
117+
118+
119+
120+
121+
122+
123+
124+
125+
126+
127+
128+
129+
130+
You can’t perform that action at this time.
0 commit comments