, ~6 \6 n i6 {8 I$ }8 v9 g public final Class<?> getClass()+ o: k! h( s+ ~) F: G
1' S7 I4 h! s8 F
Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. / z6 y( z+ B6 f1 s% L3 gpublic class InstanceofTest {! g5 O/ X7 r0 ?, i, W. T
public static void main(String[] args) {5 X2 z4 _+ K5 H. S, A! r
Father father = new Father(); * r+ i2 d! c" O1 ~# t; k, M System.out.println(father.getClass()); // class Father* m2 H3 c: w# ~
Son son = new Son(); , K6 \+ P u) `# E/ T father = new Son();+ X& b" P( B- n; U! K& }: I
System.out.println(son.getClass()); // class Son , W1 A$ j9 m% Z* Q+ F/ l% Q, Y" a System.out.println(father.getClass()); // class Son ) i2 o4 n! c" y; |+ s8 f& q$ j; g. b& ~* h3 L* W* k0 `: d! S
} 4 j' |* w# X0 c) s/ l}8 z5 J. p( O; U+ [# n3 W2 J! ^
0 P# o8 |+ Y9 z- V2 f# s$ m; B
class Father {% D9 f# @3 ^! J9 I
4 Z# R" {. I2 L5 g7 u9 i}- V) w* w0 O+ @8 K& C* ]
0 t k& u3 x* s# R* b9 B! ~
class Son extends Father implements MyInterface{$ e: V/ I( e! h+ m; G8 y) K3 s' f5 ?
& l8 D" Z( R+ n8 o) ^1 U" }0 [
}8 f1 g8 L+ T+ A8 I) n, ^
) \; l3 o, B& {3 n
interface MyInterface { 3 \/ x3 ?' }/ s# h L8 P0 o2 N s$ k2 s1 W# {/ P0 ^
}" [) n# k" X* e% y
1' I" a: p2 ]7 \6 `# `; @
2% ~- C e5 H1 t6 F8 a' T! y, u# n2 p% E
3& C8 r/ s1 H3 f) R9 V
4 3 j. ]0 B' _ e2 A+ @& F) M5$ ~% ~ W$ p6 n* E
6 7 e `4 f: E c4 z/ ^7% Y/ J4 a/ ~' s
8* _5 q$ B' q' B' a, z4 u1 D
9 $ A3 M0 |& y5 S$ K0 A1 y; ]10( q# L w. w! w& e
11 % }- }( C6 }8 k M7 H% M12 9 b2 V2 }: t; F0 z13 S/ j. c, d& E. `
14 : |% C ]( B+ ~$ t* k1 l15 ; f4 A; Q d/ y# h+ @161 h6 k, T, U. I$ l9 t
170 J Y# I5 ^5 K/ W5 k" C
18 # f5 z3 H1 q0 ?' Y19 7 P) q: x2 f& ?$ k: g205 O* ~* E2 T4 Y1 h: W% Y) ^% M
21 ! F$ N6 s: f5 }' E7 C& c- g: V- X22 [& g0 |8 W# h1 P
23 ( U7 H- l1 _5 u观察上述代码我们可以发现,getClass所返回的值与对象变量此刻的对象引用有关。 9 S2 ?% T x" J! E3 T2 f( ?& c : f" l3 M3 \$ A6 |9 I0 Y( D相等测试与继承* [, R- N: r4 Q' c* U5 [
; j' h1 d! ?: ~% p6 F, U9 J" {
我们来看,如果复习的 equals 方法中使用 instanceof 来进行相等判断,即使用以下代码片段 ' d$ K. j" R4 t T3 _ * P9 k; P# H' R6 r% l( d% Eclass Father { ( d# k& y) K3 P- e$ K8 y$ } private String name; a2 d$ ^" |5 e4 v y! G
& O* V+ T7 A* g. L
@Override 8 M( w4 d( n, j* _! Q public boolean equals(Object o) {+ y# b- N2 _) w5 P& J4 j* R
if (this == o) return true; 4 @# f$ b% R$ H& I( A, Z$ C2 m if (o == null || o instanceof Father) return false; 9 R J& f; e; E- A! f0 g( C Father father = (Father) o; 5 t: W v7 Q A' J return Objects.equals(name, father.name);4 r0 V( x: G. K- P! F9 B
}3 e( N) N& B- }/ H3 q" k6 E
}7 i. t C8 J1 b3 e* c; C: a
' U8 q4 a' `: |9 _: Q4 Wclass Son extends Father implements MyInterface{, d; K$ R' O- d5 r0 d; n x, g
private int age;- b3 d2 e2 c) c" R
( g6 A2 m. D2 i @Override6 |- _0 r: ?0 q
public boolean equals(Object o) {9 |: f* P& N+ U
if (this == o) return true;- P6 i% p, E2 R) f2 `9 J* Y
if (o == null || o instanceof Son) return false; // not good 9 q' J' c% s( s& v/ j if (!super.equals(o)) return false;( t6 v. ~' V3 y
Son son = (Son) o;5 L# {; R4 Z$ ]7 a* t0 Q( r) K2 H
return age == son.age; 4 P! z7 V7 D" m% }3 ~7 j }' `. j" P- ^3 e2 ~: |' ]# Z
}9 t# m' y. c1 m" T, N8 S# J2 a
1* V `9 s4 }# Q
2 0 c& A% X# J5 b% Q' e35 I' F2 [5 C+ A- [& r* j
44 d6 Z# H5 Z; s7 h! s' N n. N
5! b1 z! U6 V" L
6 $ s( Q6 ^# u: _% n4 o0 G& A7) H) }6 l4 c' A4 _" ~
80 `$ A4 ^; d; p: [- w! Y+ \5 k
92 T, Z8 m+ [% z
10 I/ \9 F- n; [: C& K
118 _8 E8 j4 @2 p9 w0 b6 M
12 # M" L5 s4 {% h6 w7 b13 8 \/ A3 F! c I. l, O* h14 " D# a6 R7 \& b0 \0 s7 h/ [15 + o D, H6 i# w) M16# r5 S/ Q) T% y0 N% \3 q
17; U1 W* R' ~; a
18$ k2 t, ]# B/ M* b, G8 z9 c* X
192 N+ e" {! d1 ^- i; D6 q. }
20! N* M5 w. N4 H0 p
21 ; @7 p) k6 p# {+ q229 u7 c% y h) j
23 ' j0 U6 ^% o$ c' o) f24 + w0 L/ O _9 A2 T0 P- f8 V- G2 q5 Y当运行 f.equals(s) 判断时(f 为 Father 对象, s 为 Son 对象,假设两个对象中 name 一致),根据代码会返回 true, 但是当运行s.equals(f) 会引用 instanceof 判断失败而返回 false, 这就不符合自反性。 ' \. P) g) E( z1 H, x# @3 @( n$ U) y2 m4 j: }
综上,我们可以得出结论$ Q3 D) D2 U( K# z; L1 T% v# {
, p: a& t/ L$ q& j
如果子类有自己相等的概念,如 Son 对象还需要比较彼此 age 是否相等,则对程序需求将强制采用 getClass 进行相等性检测。" ^" w- \. z! Y; U
如果有超类绝对相等的概念,那么可以用 instanceof 进行检测,只需要超类提供 equals 方法,并且要标为 final, 子类全部继承超类的 equals 方法,这样不同子类之间也可以进行比较。) {( A: v! t# x* {' o
复写 equals 方法的步骤:) G& l; ^8 @) K7 d
- u2 U' f3 i3 u& L$ `! L现实参数命名为 otherObject ; G! n9 j3 q4 ^$ T) o3 A6 ?* Q检测 this 和otherObject 是否引用同一个对象: if (this == otherObject) return true; @" E2 D$ E% L7 c3 g z, K: V
检测 otherObject 是否为 null: if (otherObject == null) return false; ' j' k- N% D6 b" G1 m' O8 A9 f比较 otherObject 与 this 是否时同一类,如果 equals 语义在每个子类中有所改变,就使用 getClass 进行检测: if (getClass() != otherObject.getClass()) return false;如果所有的子类都有统一的语义,就使用 instanceof 检测: if (!(otherObject instanceof ClassName)) return false; ; x/ O {: d$ G! Z1 k将 otherObject 转化为相应的类型变量:ClassName other = (ClassName) otherObject - E, Y6 h$ I: m进行域的比较,如果是基本类型使用 ==, 如果是引用类型可以使用 Objects.equals(), 如果是 数组类型的域, 可以使用 Arrays.equals()进行比较。; [) Y( X6 N* z2 k' U( ?. M! E# W
hashCode 方法 ) Z3 p) i. d! _! W! Y5 \! G, A1 g9 K" y: B7 F, u, U, w
散列码(hash code)是由对象导出的一个整数值,散列码是没有规律的,不同的对象其散列码一般不同, # d) P2 X |( n' A; ^1 G K7 ? - _, [7 P. k7 b7 n7 ~The general contract of hashCode is:2 Z& e" j0 d' d
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. ' M: w6 A- p% }; T& i' `If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.3 d" N) ?. ~% z1 Q* `1 ?, n
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 4 c/ H7 l; _1 cAs much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)[2] $ V0 d! \% R- J2 h" {% t- R翻译:! _# b2 t) e* k( i* ]) q3 z! {
在一个 Java 程序的执行中,无论何时,hashcode 返回的值都应该是同一个0 L2 j4 k- U. l8 r6 ^1 p3 Y) x
如果 a.equals(b) 为 true, 那么一定有 a.hashcode() == b.hashcode()* p% ?( u a6 A- j p3 t% K6 @
如果 a.equals(b) 为 false,那么 a.hashcode() 与b.hashcode()不一定不同 $ G1 T* }- S" dpublic native int hashCode();* h. e* r3 } h1 C6 T# [
1 6 U6 L: U0 L) L2 I% {# A+ y5 q& H以上为 Object 中的 hashcode方法,我们可以看到这是用 native 修饰的方法,表明该方法是使用了 JNI(Java Native Interface),使用了 C 或 C++ 进行实现。在实际情况下,大部分不同对象会 生成不同的 hashcode(通过将内存地址转换为整数)2 A1 G( e) g1 q: ~7 d
- r8 N, Q8 {# T; D7 t: P0 R. vThe main objectives of native keyword are:[5] & N" d6 y l9 A1 ?2 N [$ h+ Q% v' _9 R, R3 M
To improve performance of the system. $ j9 }* D, M; {* Q* L- v5 pTo achieve machine level/memory level communication.' Q$ k/ f+ b z6 h2 T
To use already existing legacy non-java code. " O8 b4 E7 E7 ?/ @测试代码: * ]/ g! m. G+ Z7 B$ x; u: N% S! y$ x# L2 {
ublic class HashCode {3 A6 h% q- F/ }
. G; u0 @# l* k0 A$ I0 g public static void main(String[] args) {9 F' { M: j" n, J
Map<Student, String> map = new HashMap<>();1 o) ^ C$ ~! S1 H
Student s = new Student("Alice", 11);4 \' A! O# ?/ @) c6 ?% \0 b
' d% A/ V: `4 L2 m
map.put(s, "Alice"); # f; o( h( X D. V. f map.put(new Student("Bob", 12), "Bob"); , Q. W1 R4 T% E. N4 l map.put(new Student("Cici", 15), "Cici");* r! l4 g% F1 J1 ~+ }4 g4 k4 ~
" D! W. _, y, C5 R+ N2 J- d if (map.containsKey(s)) {; L' t1 l# G" D2 x
System.out.println("bingo"); // bingo ( z+ t i1 Q6 p }: W% i3 W: B/ Q+ w
# E' a8 l$ _' p
if (map.containsKey(new Student("Bob", 12))) { 7 _( s* B3 D, q z System.out.println("hello"); // hello3 P) G3 f2 R; R: Q$ }: R, A0 @/ Z
} # e7 Z. Q- H/ \; K- s4 \ }$ d8 c, P0 A& X0 k2 Z1 ^
2 K7 W+ M" K9 i& P
}5 }$ E- x, s7 ]( P
) H8 I# j. T" n" jclass Student {3 m% I( X! p; C- g( S8 @9 ~
private String name; , Y l; m9 v0 [ private int age; # d2 d- W) o6 ]8 @- t e6 i9 k4 |- k) I# L
public Student(String name, int age) { + ^9 p/ s; a% N$ b1 g this.name = name;4 w; i2 E" r3 P$ S4 e- s' r
this.age = age;5 u3 U" P3 C4 `- w, a
} 2 e) _: m% A; Q$ v- Z4 ^& T6 o $ V6 F5 f1 B6 {: g @Override9 Z& N, y5 H T/ ]1 R
public boolean equals(Object o) {3 N' Q g( g# S5 x) F% z
if (this == o) return true;* X" u- b1 a* x; Q
if (o == null || getClass() != o.getClass()) return false; " N' o+ E3 D- h7 p- r: A; ^ Student student = (Student) o; ; X2 K9 `% e8 q( J return age == student.age && ' L q+ x \' c Objects.equals(name, student.name); " M( {8 y) ]0 c# J3 i, z) t2 w' C- i }9 }/ x! m7 W# G; k
1 a5 v L2 `0 J @Override9 g. o# j8 K$ S- z9 W# s
public int hashCode() { 1 j3 t3 J6 y8 w$ @ return Objects.hash(name, age); " i: f% t4 d+ n4 \$ |7 E }: Z9 |( l; h- }6 ^! a) {
} 0 N M+ J- t; E6 x+ _2 u$ V1 4 i# b1 w- n" i8 j1 \2 ) w8 c; H2 h C& ~- K2 i35 J# M. f8 q& @$ x' M0 f. ]
4 " k( j. c" ]: _5 / ~6 D; t, ], \8 C$ A1 n( l; y6 % E. T2 o) K& [6 t7, K$ P; A- w P4 Y# C E+ q* m* O: G: G, d
8 $ i6 x4 d/ Q6 F( `6 O9 % V1 `: P$ W9 C% d10 7 {& g# I: }* H11 0 L: J1 c% r0 G: J* Z12) B3 F( U0 G& x. ?- N. g! w
136 J7 z; d+ h; F2 {
149 l4 k U( [0 m4 u5 v4 x& e
15 : k, {1 z/ d2 K7 R9 {2 _16+ P: z0 u; x+ E! w4 a# G/ Z
174 J. D) V& M- l9 F( F: \7 c
18 0 d$ F3 A x% ]19: e7 e/ C1 d5 R) h: p% n6 i+ O1 Y
20 5 C+ k+ o* d0 C8 Z4 J21 / ]0 f7 y$ X9 e* [3 _22: z/ n! c. u/ `: }$ ]& C1 ?
23 2 l, _; d1 y5 r" h( c243 R5 O! Z9 }) Q$ x
25 h/ _# `7 i! H- B
26 . v' t" W1 Z; v27: p- z; I4 I* i
28" [/ g+ u1 z/ }. ]5 M8 a
29+ h9 Y4 U& F3 A0 f3 K
302 |8 e0 J! \4 i7 D1 M$ G
31 $ m* D' `2 D0 l3 w, E328 B, ~/ N8 _# J% s7 Y, f
33 # j/ C2 m5 F [/ \: I) j( B34 5 ]( |: i" z4 F$ B/ k2 B6 S G" g( \35 8 I, s+ y* p1 Y" T7 m( z4 j9 v9 c/ W366 U \8 l# g" ~9 f: l; l& P# k
370 P# B4 j/ @2 f% A5 l) z0 i9 j
38; I( ?; F* |" H/ v! t
39 * R% q% I; c, b# y/ m }) v3 P+ L' l40+ m% a: O; z- u: q; x7 v. ~1 E
41; J# A: C. F/ ^
42# R" G# D+ a2 K- t6 I1 M
43/ F3 u3 E$ {& |& T, f5 ?! p
44 3 f7 C# |+ b$ M# B- \8 r* ?4 A" z+ }上述代码测试了,必须重写 equals 和 hashcode 方法,才能正常作为 map 的 key,如果有其中一个方法没有重写,都会造成 “hello” 不打印。 # Q8 B0 ?; G, k: E% W& ]" A- _6 i8 V; V4 Y4 f
clone 方法4 S1 {0 f: d4 O& x9 W+ |; B2 z
# j7 ~8 N# k! C7 {9 O
protected native Object clone() throws CloneNotSupportedException;3 v6 V1 t- A' L( Z6 R4 R2 F5 b; I z
17 R& y; G; h, z8 k6 G
观察源码可知,clone 方法是 protected,子类只能调用受保护的 clone 方法来克隆它自己的对象,即只有在 Person 类中才可以克隆 Person 对象 8 L4 {+ z( R; Z9 [" p* J, N' z& o6 i- {* t w
public class CloneTest { ; W0 J. b# F5 f: f public static void main(String[] args) { 7 J% t5 p( ]7 v$ N$ j8 R0 T8 q1 o Person p = new Person();" C1 r' N. |5 @ s9 K. f9 p+ p
Person p2 = p.clone(); // Error clone() has protected access in Object; X* e y& a* M
}, p! w# Z% G- l" F% e; d
}$ Y. l( [6 e* }
5 i- J5 Z# ?5 w6 H0 `" z/ |class Person { & ?7 k H0 b7 r6 W" O0 E* U private String name;7 u3 z1 ]! n* S0 Y# t% w
private int age;: [7 A: ` x( X6 q1 p# H
}6 ^, H* o6 }9 H8 ?* Q2 L7 w" U8 x
1 9 S5 H: o W, E) n2 9 e/ o0 P. l; M! Z, M3% F: W2 C# _5 C' `. s9 \& Z; N
4 8 ~5 \, x6 z7 X! `+ S& I5 # k' v" ^2 S- d% d8 x1 b$ _" }: b6 5 ? w' I+ ?2 Q! D/ m7 ' }& @7 }& F+ m8+ j- z2 O8 N7 G) w: ?
95 M, A; D- A2 ~ ?8 a/ Y: i
10 ' r- M1 C$ x1 ~: [117 N" e+ R3 g# k7 H( ^9 J
我们可以重写 Person 中的 clone()方法为 public 即可调用 & V$ M* p6 k2 j- b- ]8 E# r; E3 W2 z$ a2 H& w. N# q1 G* {
public class CloneTest {9 k5 p2 H/ E' p# w" n
public static void main(String[] args) { : C8 y) {+ B6 C) y2 @ Person p = new Person();9 E! Y; p3 g( ~. e$ T
try {; h- B+ M3 o3 K9 m- \! C
Person p2 = p.clone();; Z" S. p. R9 Q }/ J
} catch (CloneNotSupportedException e) { 8 q( g/ z+ D8 p5 _8 n e.printStackTrace(); + `* R/ Y! l3 a3 Y: ~1 ?; { } - T7 T$ e( e$ U1 F& C' F3 M } : x+ o# m! n! H! A$ a}! U+ y5 w& D: |: `
' C0 \! ^9 G' m$ [* x0 Nclass Person {6 r( f& D# s) T) t( c# ?
private String name;1 s _. z9 y* Y2 }8 H5 u
private int age; , H7 ^: ]) Z! h7 x/ I0 L% }3 \3 z( P' G1 |: R3 z' e8 B
@Override8 f! K; K, k; X) h
protected Person clone() throws CloneNotSupportedException { ( U# i' D9 M: Y+ ^# F return (Person) super.clone();2 K* x8 v: j; F
}1 p/ F' a0 Z8 o( J& c$ s& b) L
} $ V5 n' v. b& H1 d* k& f$ ]) m( ?; ~
24 o3 L$ V0 L4 ` b6 f
3 8 I7 N! B6 ]. `. N& M* `9 ^4 ; W @' c! ~# A, j56 d5 ]' L! z# J7 e+ v
6( ~4 B6 x) {$ J' A( z3 q
7# x0 g0 i* B: u, y
8( [ U1 x# V) V2 j( j
9 9 X' z- ]* F- ^10" j" K/ x0 b( t
11 " j6 i6 I6 V6 x3 `/ a12 ( Z p7 _) Y% S# f( t a4 e13) H0 _; N1 w' u1 F# t; {
14 " P9 t) l: n% L6 S% W& j6 e, p& ^15 . _! C5 E5 n8 m/ O# s1 y, ~16: s4 I. E: @% C" w. S; x7 }
17: w1 U' A' k5 A1 s4 n# Z1 t7 a! g
18! B1 u; _: {+ j% B
19 ' }$ b2 G9 U/ ]1 M$ C7 a; n/ ^% y: Q20 " a+ o, a" B& h但是当我们执行代码时,还是会抛出运行时异常:java.lang.CloneNotSupportedException,这是因为我们没有实现 Cloneable 接口,这个接口是一个标记接口(marker interface),只是作者了解克隆过程。* {2 W+ i7 X0 G* a% v( ]