; w0 [; r& h7 P浅析Java语言Object中的equals、hashcode和clone方法 4 h; ?1 \' z6 Q% `2 F' I* | ( L( I: P* |& a! s" L! w1 AObject 类是 Java 中所有类的超类,在 Java 中每个类都是由它扩展来的,只有基本类型(primitive types)不是对象,剩下的引用类型都是对象,包括对象数组或者基本类型数组。7 y3 p- H9 r i( n% `! }; c
, ?9 @ C6 q# m% s) p! D# g5 D+ A
Object obj = new int[10] // ok0 j b0 i; a; k5 I2 c
1 ' l, |! k% o: J, kObject 方法概览: b- x g. I# P- K1 h; h
. m8 Z3 E9 |. _" c) F$ [public final native Class<?> getClass(); 6 F) l5 t8 D+ A. lpublic native int hashCode();5 S u% c& K4 \8 f8 X% n6 H
public boolean equals(Object obj) {} / Z6 o6 N# p. W4 o+ m0 x4 b3 Rprotected native Object clone() throws CloneNotSupportedException;% O( y- I4 ?0 j) q2 K
public String toString() {}2 h& A7 Y; {4 Z! k' C1 ?
public final native void notify(); 5 B/ e' G6 G( O4 ~% D- u# l- u+ Fpublic final native void notifyAll();1 g% W" F# U* N! k G. R- Y! V
public final native void wait(long timeout) throws InterruptedException;, o& k2 E/ O0 a5 I& c1 J, h
public final void wait(long timeout, int nanos) throws InterruptedException{}; L0 f( y# b) T
public final void wait() throws InterruptedException {}4 Z# p, Q5 Q7 Y$ i% \0 ^1 |
protected void finalize() throws Throwable {} // Deprecated! f: R8 S# L9 M! w( f
1 $ f+ _3 Y# x' F5 k3 A6 [( N: s2 ) e |9 _* i7 z4 P6 w- S" b3 - d6 ~- L& e o/ N5 X46 C- n% t4 W9 D1 s8 S- h* ?! q
5 7 Y8 j8 b" ?- B6 / j- k! s) M3 @71 j3 u, }* i( f" x7 a
8 # \8 i: k+ D7 y: e! a! n: h90 ?- U) k5 P9 V( y, F) `
10 9 E5 ^8 r' X% X11 1 `+ A; M$ K' E' T- k. Xequals 方法8 s) {; K# ]3 j- @4 n0 T
$ m2 }* y, G+ R
public boolean equals(Object obj) { " S" M, O; H& S2 z3 z9 Y$ i9 w return (this == obj); . q& Q" [: N( j1 r; F- l9 J} # @6 T/ O6 H" X$ _5 t7 D1; z: f* t! E. b2 e4 A1 \; R; y% v
2: G% E4 ?. K& D6 x( x% K8 n$ v, T
3+ i8 b$ {8 m, `8 e/ w, z, K& H
阅读源码可知,Object 中的 equals 方法是比较两个对象是否有相同的引用,但是这在有些情况下是没有意义的,更多的时候是想比较两个对象的状态是否相同。 8 J% L; l H" T$ V* A 5 m" u$ I9 G( k: F8 M# B; iThe equals method implements an equivalence relation on non-null object references:0 j( q2 T% A1 P: F
It is reflexive: for any non-null reference value x, x.equals(x) should return true./ ~7 b2 E( g* i
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. ( F# D! K$ |6 K1 H4 d O8 |It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.3 N% y- Q# L9 q2 Z1 I
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.( |5 y$ p# u8 S/ B* S, x, g5 r
For any non-null reference value x, x.equals(null) should return false.[2] 6 E" l; w$ `# h由 JDK 文档中可知,实现了 non-null 对象引用的相等关系的 equals 方法有五个特性:reflexive(自反性),symmetric(对称性),transitive(传递性),consistent(一致性),与 null 相比返回 false v4 T9 S+ P0 _6 J% X
" E( X. c$ E, _7 `9 P
public class Person { : P/ d+ z: D. N* s T" t) L6 J6 p private String name;+ T) E& C/ { f# p
private int age; ) c: c- C) U- ~" q& C; w! n. U0 J+ k1 F9 @
@Override ) {0 a2 F# g/ T- N! A public boolean equals(Object o) { 1 ^5 e/ r0 a7 |. h% o3 r+ { // a quick test to see if the objects are identical & v6 c( k+ B! Z if (this == o) return true; \; F: m: n {5 f2 a. h/ {* E- i( ]
// if o == null or the classes don't match, they can't be equal 1 s2 |% f4 F; ~# v: q if (o == null || getClass() != o.getClass()) return false;8 x( z. v9 @0 ~6 W
8 e6 U [% M4 |' c // now we know o is a non-null Person " u$ `- v" W/ R3 m# O Person person = (Person) o; 9 q B; v, k: n- }- S9 y: Q; n# c) b1 z. D , G5 l7 Q3 `* F, g0 L // test whether the fields have identical values 7 T# ~" d: P4 \! t7 }6 q F if (age != person.age) return false; % P" X. ?9 [* H$ O return Objects.equals(name, person.name);- X0 N0 ^' E5 h& G% P
} / q0 I+ I" X4 F}6 D# q1 q( u1 j* g
1 " _; a8 Y* H( h2. W/ \ s0 f6 @4 a$ A0 ]7 f0 ^
3 / `* d7 E2 U/ e5 g( ?44 G* G% O1 o6 `- ?1 d- D
51 Z2 N3 d$ a9 j0 M# |7 L
67 L/ Q9 y$ \. [* ~
7( Y! s; C' X' ^' P4 O
83 H7 Y+ q0 l* d1 ?
9, C/ y( P% c2 X, Q; C) z
10 5 _: ?$ I& Q3 B6 Z3 W+ v112 A" [7 s/ ^! u( `+ U
12 , B) E0 l: |& U( H q6 I* b13 5 e& G+ I' M# `% \/ c14; |- S# [3 Q9 F) `3 m) \
15 . G. F; M) g* D, B5 j, e2 f# u+ d$ i16 * {# ~; G, b P1 ^6 c* ^' I17. |* m) R# g! Q3 m
18 . b& u4 c0 C/ |" K! g19 ) j. n+ j) K( l# D" q" L注意,比较可能为空的变量时可以使用Objects.equals() 0 J8 _: C+ R, H# B * i% C9 n+ ]6 z子类的 equals 方法:/ K* S% [/ W* ^5 g5 M: F$ D9 z; u
( I5 ~' W. h. Y8 c" tpublic class Student extends Person { 1 u" O' g# t& F private String school;3 ]# h/ n; w: Y% `/ R' F$ {/ H
( o% D- q& K- C7 f. I
@Override( f/ j6 S# S' r3 M. \, b9 [
public boolean equals(Object o) { 0 Y0 B. {, I% \5 ^% U2 Q! Z if (!super.equals(o)) return false; ( t$ j( j& j# K4 Z( K0 M / R* } ~6 Z4 |! q- _- d Student student = (Student) o;- F: [2 H# w0 Y- G7 m2 B: U
4 n8 w4 O) r# ^. O return Objects.equals(school, student.school); ) U( \/ g4 c6 h$ ^2 w$ x }6 t$ {3 Z1 ^: x2 ?9 V
}6 S% D$ l9 E9 n! I9 ^
1 & U+ m9 R) b3 S! F9 Z2 8 |7 g7 x3 I) {# H7 |( ` u3 ! F* c0 ^* g8 z+ \* _4 ! ?$ c! M5 N7 C' s5 2 U* {6 ?) p$ T; g, S/ `9 ^63 ^2 b$ Y) ], |( D, j1 X7 _% o
7 ! s3 z* m* |8 G y* k) f8 ! M3 k4 g/ ^* _0 i0 \7 O5 \5 _9) R0 A/ L0 H1 Q4 e
10. X7 `1 B4 ~/ n
119 A" N& }# m L( U% u6 a
12 7 K: S& ? q ~; H; S7 J5 w( z, S0 v我们在重写自己的 equals 方法时需要注意到自反性这个原则,即x.equals(y) == y.equals(x),在前面的代码中我们用到了 getClass 这个方法,在比较过程中,当发现 x 与 y 类不相等则返回 false, 在这里我们必须将 getClass 方法和 instanceof 方法进行比较。 * I B# H/ I; k3 v( S ! k" r) n, I V1 y9 [0 p! iinstanceof $ B) i: Q3 c" D' e0 N" t4 z9 P" k' G3 v w, [& n' z# l
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.[3]) `$ I0 s4 W8 Y0 J9 D& y% j
简单来说,instanceof 是用来将对象与类型进行比较,我们可以通过它确定这个对象是否是一个类,一个子类或者一个接口实现类的实例。) o# F* G% P7 ?% X+ ^* \- N& @1 z0 \
& x6 ~1 m6 F; h7 F
public class InstanceofTest { 0 `8 g1 h$ c" V# p4 J | public static void main(String[] args) {4 q" b0 l+ d& S- M& R
Father father = new Father(); . p. g( S0 F; K System.out.println(father instanceof Father); // true 7 @8 w% N8 ~2 Q' m2 c System.out.println(father instanceof Son); // false $ p; i, h! t( K# p% |1 [1 ?9 W9 D System.out.println(father instanceof MyInterface); // false # V$ j4 L& N; f" V9 h) B4 ] System.out.println("------------------------");" m' q6 s) E! V
. y! w3 h% b6 R9 o$ m4 N( r. y Son son = new Son(); 9 u, |2 K2 J/ p. v3 m System.out.println(son instanceof Father); // true ' _' d/ {1 d5 e' W5 c- u' l% [ System.out.println(son instanceof Son); // true) k3 T/ r- u0 W5 l$ H
System.out.println(son instanceof MyInterface); // true. H3 a3 H2 ^7 Z7 v6 }5 p3 I) ?
System.out.println("------------------------"); 5 \/ V1 Y4 e) ?) W4 g7 S' C a* H0 n. _. O3 ~9 F6 J! k! Z8 r7 ]
Father father2 = new Son(); & o9 _8 w% [, Y! n System.out.println(father2 instanceof Father); // true' A* F+ A" X% A+ s2 i1 B
System.out.println(father2 instanceof Son); // true 0 a) F" d+ C6 K, G. L5 {7 ~ System.out.println(father2 instanceof MyInterface); // true/ V5 b2 G9 L* c' V: ^
System.out.println("------------------------");, W: y: ^# R" J% b. T& d
}4 n! m+ b* R. k# u
}) {/ [: Q+ d( D; L# b9 b- ]* U
class Father { 9 d+ o. ?5 G! S% f1 K' {. z0 g 8 p# l/ o& H9 R0 M- }" w8 e# ]}8 L' G( B/ g' a
/ r2 [+ y2 C M6 \# Y. `class Son extends Father implements MyInterface{ + v9 V' w! s2 o5 B, @. s. y - W) Y& F* P' ~6 d0 u5 Q5 g# J* i8 ^} ! ~% C8 K1 N& [, c, m " g0 Z4 C- e% Jinterface MyInterface {: C& n8 e5 Y8 ^4 f7 r
. e4 }1 x( I! a: K6 O& W! |}/ o. W+ e& ]; `3 e: e$ E/ Q4 H
1* J/ Q* `% b7 e
25 m3 Z( `4 ^. e3 i1 R- q _
3 * D8 g2 T1 t% O4 ; P" L: y6 m+ p! A% e# G$ k5- s' n* I c1 f0 v- M2 ^
6. Q5 \5 h: A4 o0 L1 ?/ _9 v
71 }% I, I7 ]5 t5 U" w9 l# G
8 / u3 J- B. _* a p5 ~# O9 ( g6 C- S' V. Z7 k5 _! e& C10 9 c' |5 \ E7 d118 c& \( O$ K U& ^$ r
12% T+ W0 P: i+ B
13; |% |6 U& I) J$ F' \& S( m! k, w
14 & O5 _) W0 { y- n15 0 Q+ I" |* _' |16" k J* a: U( V) i. u. V
17 6 p; ]1 X: d. _ G$ d6 N$ c/ b18 8 d2 W1 w w5 }+ t- H$ m. L C19( R& y* G% l; s: j; N
20 2 }' W3 p: `; ?: {- d- \215 l3 Z+ Z; g% L% i4 w5 y$ C
22# L3 k% j! m1 t' T+ o, M. S
23 9 Z4 r# Y& C0 C5 M% c247 i; L4 B1 t( j) u
25) M. Z! {% a i* T
26 e- e& }% {& F) G: g27 : w, v5 X x* c- }28 1 g+ }0 |0 o' X3 G; W29- D8 x) j2 {) n7 V+ H1 s
30 5 J8 }4 o8 {* K! d( r31! M% e; D7 B: m& @
32 , r) ]9 ~. n, G1 S. G- n. \4 n上述结果我们可以看出当父类变量被赋予子类对象引用时,其结果和子类变量是一样的。& ~; \2 n$ v$ b9 r6 j
* k- P9 b$ b* _% g& b9 z: `$ z5 qgetClass % v& C5 k7 l6 G( t+ N* T0 b. [8 }' l% ?. w, C
public final Class<?> getClass() , B/ {& \8 @# Q I14 {$ z1 {* h/ T
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. c$ F7 r* z, e$ q# Gpublic class InstanceofTest { 9 L9 P V: |$ y% v4 |6 M public static void main(String[] args) { - N9 _# a. n' _' b- Y$ a. ~8 K8 u9 n Father father = new Father(); ( p& S; |) [, z7 w; v System.out.println(father.getClass()); // class Father" w' R9 j. ` _$ H3 G0 ?
Son son = new Son();& `$ B+ E+ I i( I& a( G# Q% S# \
father = new Son();2 y! K5 \" A8 Q& f, V
System.out.println(son.getClass()); // class Son $ j& x) t. j0 I/ h System.out.println(father.getClass()); // class Son ; m3 P6 |( N% ^8 G. q# r& N , c/ B7 F6 I; @ @: P" _ } + u( k. M. Z' j" v1 J} : H- {# f$ g' }1 y7 T. A/ B d; g9 C; s$ z
class Father { . i8 x7 s+ m j" D/ x7 H& W5 h6 i0 v8 A9 N
}. b0 q& s& g5 I. h$ B5 ~& u x9 U
/ F) R' u; v; L$ Lclass Son extends Father implements MyInterface{2 O" n1 m, R* Z* \
* I, B1 c, ~$ n% F散列码(hash code)是由对象导出的一个整数值,散列码是没有规律的,不同的对象其散列码一般不同, 7 [/ Q% d8 e% B; g" r" J1 B6 c+ l0 B. O { n
The general contract of hashCode is:( o" b/ j5 X1 g0 w1 H2 N' f
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. / o: H. v. H' x; I, r+ V- gIf 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. ' q; H; r8 S9 e" d, P: dIt 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.) q# a6 M/ D3 T* l" B$ m. w: D
As 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] 3 J$ R) J+ O3 a翻译: * t: r8 ~* E7 u2 |在一个 Java 程序的执行中,无论何时,hashcode 返回的值都应该是同一个 + c3 O" @9 B5 U X4 q7 u: n如果 a.equals(b) 为 true, 那么一定有 a.hashcode() == b.hashcode()! [1 v; |/ i. ~$ q* G
如果 a.equals(b) 为 false,那么 a.hashcode() 与b.hashcode()不一定不同 9 ^* Q' I+ t3 W) i* T- e0 b# Opublic native int hashCode(); ; C, H5 J% ~* F* I1 z- d6 v1; z- z1 f- U- ^! V
以上为 Object 中的 hashcode方法,我们可以看到这是用 native 修饰的方法,表明该方法是使用了 JNI(Java Native Interface),使用了 C 或 C++ 进行实现。在实际情况下,大部分不同对象会 生成不同的 hashcode(通过将内存地址转换为整数)$ ?+ H4 J0 ~* e4 Y
. L2 ^ S, n$ u: D2 P- j
The main objectives of native keyword are:[5] 0 v( p7 m1 `- ]* e7 f# L* [( l: R' s
To improve performance of the system. : D$ f) U# K% c+ oTo achieve machine level/memory level communication. / [+ b0 r; q/ |* M) FTo use already existing legacy non-java code.$ q' ?* F, l4 E7 o# E& j- m: l
测试代码: - J6 \. B4 P! I' l, E. `; q. q5 }9 S* [- \1 I9 w. Y8 p, {+ i' ^0 U
ublic class HashCode {) Z/ x T9 |5 p9 o& W
" C# B: ?7 b% H1 M public static void main(String[] args) { ' c$ z, g/ v8 a! T* G- T1 y Map<Student, String> map = new HashMap<>(); / k" Q5 R8 @: ?7 Q Student s = new Student("Alice", 11);) h6 I) \* r. M3 _
6 A! `: j, r, j( ?- ?
map.put(s, "Alice");: ]+ y/ }) r. B% c# D( N! Z
map.put(new Student("Bob", 12), "Bob");" o% _' C; j2 b6 c: B6 _' f
map.put(new Student("Cici", 15), "Cici");0 p O2 r4 ` `; D$ d! h% Z
, c3 r2 l5 \& `! ] if (map.containsKey(s)) { $ R: e, R, n4 R/ { F) ^ System.out.println("bingo"); // bingo ! W) D' Y W" U/ |9 ^' n } / N# q+ n1 r$ j9 s , ]% _( k: C& O if (map.containsKey(new Student("Bob", 12))) { 3 V. I L2 K" L& @& J+ g, k P System.out.println("hello"); // hello 2 N: D8 U) R4 ^ Y" a, E! E. B }+ {0 ^$ C. W: \# l
} X& L: \2 T- u0 P4 l1 t 9 q0 K2 A9 V. U* F0 H& p} # h" U; c) d! i0 | M' }* S, T6 J: d+ n7 @& r0 @0 u5 W9 y+ t! B4 n
class Student { # N$ O% i- T* j& @. {% g8 F private String name; 4 J+ M0 c) b& g private int age;$ p L3 h) G) y( |: x i' x
- Z; S6 L5 i0 H; s* G- | public Student(String name, int age) {) U. z" s* L( _# m% ^
this.name = name; + M& Y# n K! t' H this.age = age;7 R; n$ I- C3 Z5 N7 y$ M
} / j( p# X" A7 l9 b2 [8 N7 i1 I2 K) V# T! E, Y4 [# W( @7 g
@Override! Q# W* C9 Z* T" y( q# P
public boolean equals(Object o) {0 l5 F% \ f% x2 t2 P
if (this == o) return true;+ b' g3 t9 `9 ]% C
if (o == null || getClass() != o.getClass()) return false;7 d9 e+ V& U8 L* [: `: Q7 B
Student student = (Student) o; 9 i4 n' k6 K7 D7 l) e" r- n$ [ ` return age == student.age && " [8 s# J& x2 b) r# L# k+ A Objects.equals(name, student.name); ! o' X7 F' j! [% x# R; G) U } $ I e' L. \- o- {5 I3 {* V, p( E) i O% P2 c; B+ _7 Q& H4 ?
@Override 0 D x. x* H% `; y% J7 ?- ] public int hashCode() {2 m! \% t& b3 p; {; E; M
return Objects.hash(name, age);2 ~. I& c; E! h& _6 G0 J2 b% ^
}- H7 H1 }' O' P \* C5 f* K
} c. e5 f b4 ~* [8 t% A- r
15 U+ Z6 \$ A! i5 w
2 8 o' j; H& z: F1 J! ~3 7 a O' w! k+ A, d2 a5 W0 `4) K) _$ g; c5 }2 l. Q" w2 A
5 2 z4 d0 u5 M( e( F$ k- y z- C6 c; \( b* L) M1 A1 I: b! v8 n t9 @* I
7 ) q$ j& x9 v( ~6 C88 H, `5 S: o6 j3 N: u5 E
9# j7 ^1 A: ^# E9 F8 }! d; i) y
101 J( u' U# _8 x2 H+ m
11 ( W" ]* m6 f* w6 D5 ~12 ; S4 f: J; O9 z8 T( g6 R( E' e13 1 J [) S$ A% z% |- N2 C/ B2 N9 _7 [14 : T% M* s" `! k, A15- x3 l- R: D; K) P# F5 `
16* ]) z5 p& E! h
17! T7 h x- m6 F% y- e
18- \& {' A0 c- ~
19, o4 R1 V" }# s/ A* m% y! P
20 4 f# W$ _) K" Q/ {; `7 Q- S+ b219 y7 F3 v4 b& h8 n
22 & M, f+ Y" @1 H+ y( q. [23 ! G7 W3 \; U" Q: k# I: s( r24 , E- S* B3 I2 Y; \25 t1 P% H5 l# O+ E; w, D# o: o
26 * Z! {+ ^+ P$ F6 h/ u27, Y: ]! ]% r% H) B; g
28 % R& t$ V6 l$ K& G4 N& A, ^+ V29# `. N; m# E y7 e% q9 a6 }& z0 k: G
306 S6 P8 v P& f# J$ C, I: M* w
31 * U9 d) v2 e0 M3 O32 6 n- F2 s, u4 S$ t33. H7 K% B+ ]- h9 u
34# @* i2 {3 N/ o/ _
35 # k p% h$ [. e; q8 x, L" E! y36 1 E m6 j+ h( w5 ^# G) n37+ ^) J; Q, X2 J1 s
38 5 `( G, \% E$ P4 [- Z. D9 [39 3 P( T ]' [6 j( Z' L3 B' W) R40 - _! T# }6 A' q8 I7 V2 D41 c/ ~1 b7 Y1 V9 s422 n" M. `$ T5 L
438 m" h" \6 m( f( Q% b
44 - p, o- \9 `' b* S2 B" _4 D上述代码测试了,必须重写 equals 和 hashcode 方法,才能正常作为 map 的 key,如果有其中一个方法没有重写,都会造成 “hello” 不打印。6 A3 n4 _, @" P1 q/ J
' l( g3 J) G- S: p7 ~clone 方法 1 P- l r- [2 `; r; P7 G+ U c: Y9 [3 {* J
protected native Object clone() throws CloneNotSupportedException;7 q+ k0 Z- z% F; B/ S: d7 u. W
1 5 D' Y+ L2 ]& }观察源码可知,clone 方法是 protected,子类只能调用受保护的 clone 方法来克隆它自己的对象,即只有在 Person 类中才可以克隆 Person 对象 8 I7 l) C4 L; z! A; B7 j/ l. y5 a % }: i8 q5 B3 R+ z- x6 Fpublic class CloneTest {0 Z( b2 P4 |& |" I7 z& n
public static void main(String[] args) { n! u" n+ k) m& E
Person p = new Person(); ( H' A( T2 O; s Person p2 = p.clone(); // Error clone() has protected access in Object a+ m9 H5 r' P, s1 @ } $ O. V. A, g; n8 ]& `} 1 u* s9 j2 Y3 h: P) w' t( g/ ^ / r8 T9 P4 r! q6 _8 e( X) uclass Person {3 l& `2 `; i* s/ L' [3 I
private String name; , R- c$ g3 K4 }( x6 g+ o private int age;& B: D7 L9 r i: a0 x R. u
} 2 {# ?; e6 }+ x( M" \1 ; X7 p* j6 j9 F2 1 g% S, `+ J* V3 ' c& @% D/ w% r! H& J2 J4 S4 " r( L7 J! G- a5 h5; k$ y" E& e: \8 x) {
6 " n; a/ c* x: `* v+ J7- H. a f* ?. \) P& x: d( z0 @
8 1 F- q5 Y |5 I; a9 L3 f/ x9 & f+ Q1 P3 S! j! `10 ) ~) e+ _+ g: @, I, F/ l4 }11 8 F3 L* R7 b6 I$ F8 H$ Z' ?我们可以重写 Person 中的 clone()方法为 public 即可调用 # u: g9 G2 w" G' a 7 w- H) f! I" l, e2 q' j u5 qpublic class CloneTest {8 `4 G5 m4 ^# c, e z% q% ]: M
public static void main(String[] args) { $ T; Q1 l; I" z! y: K& C& X' e1 K Person p = new Person(); * ]4 S+ o- u' ?" l try { - y1 Y/ [( `9 W/ X Person p2 = p.clone(); B4 S* |4 y$ U H5 Q6 V } catch (CloneNotSupportedException e) {* y) o. }# Y3 n
e.printStackTrace();4 N: F6 j5 A" K8 `; {
} 0 p7 Z6 g$ Y! l, Y9 v: } }8 A7 u) S+ [8 j
}7 c! J; O5 U4 [
( l1 P- {" U8 R7 p8 mclass Person { 1 }' {2 {; l' [2 \ private String name; * d# Z0 S( S4 `2 R: l private int age; % {) O8 H$ m& i ]) v, Y: h+ u" f2 w3 N0 e* Z }: T
@Override9 P" C+ e. {! o; h$ f3 M' g1 H
protected Person clone() throws CloneNotSupportedException {0 ?8 I' ]8 l! q7 V$ S8 E
return (Person) super.clone();2 w' p& P! Q5 t
} - q$ A9 }( j% O% W0 u9 M+ h} . D B# @4 v; P/ `: x9 U+ h. T1 ' K p1 m/ m0 U$ d2 ! [0 n9 d3 e+ J) R% d/ m' A3' r3 g& `+ K2 G. I' v& h
4 & D9 A' C- u; F: Y% s; _+ Q5 * K8 q. o. ]% g( b; Z* O6- T* J. j" S" Z% v+ K& M* ] q
74 ~+ M+ g% U5 A* |! k
8/ V1 ?: ?' ~: n& t* y1 `
9 % n: p) h& x, A' v10" i* s) w) X2 ~1 W# P
11" @' m3 Y# }+ Q. h
12) c2 r6 V3 W! g; t9 d# y* ~
13: ]0 K9 g- Q- v Q+ ~* A+ @
14' |& {* G" Z# K) w* W0 ]. U, F
15" I, y0 |. P* N# w- k
161 k0 R$ J- X* c: v. W6 c
17 # ?+ D6 w/ ]4 u t: V& I18: U, o" J! W0 Z
19, L5 H- e2 H5 @7 r
209 ~) T( L3 }+ P( S; d8 I3 H
但是当我们执行代码时,还是会抛出运行时异常:java.lang.CloneNotSupportedException,这是因为我们没有实现 Cloneable 接口,这个接口是一个标记接口(marker interface),只是作者了解克隆过程。 3 x: F+ p: R5 w! w! P- a. T/ x( }/ [* h
浅拷贝7 J7 }; v: U0 r' _, D# o: T