4 H6 t/ x' f$ s. \8 a2 U) e 浅析Java语言Object中的equals、hashcode和clone方法 0 O' x9 @' K, \0 q / W# Q! T# z' i R6 h* T! ?Object 类是 Java 中所有类的超类,在 Java 中每个类都是由它扩展来的,只有基本类型(primitive types)不是对象,剩下的引用类型都是对象,包括对象数组或者基本类型数组。( }8 G4 w& s# U. T# m
6 ]0 e* R, _* Z- h- fObject obj = new int[10] // ok ) t8 C& i; j( n7 T1* w( b1 a# `3 D
Object 方法概览: . Z. F& H0 P1 }: J, `5 r# C/ V ' o5 R8 M5 O4 l, U/ x7 E z4 b% a# _public final native Class<?> getClass();, D& p% V0 L. m/ t E
public native int hashCode();( R2 @; G& p- j0 k! s( T. J- j
public boolean equals(Object obj) {} 1 M* s: M. V8 z2 L# gprotected native Object clone() throws CloneNotSupportedException; 9 Q9 c+ B4 y6 Bpublic String toString() {}6 z% t1 Y# r/ }# Q! F$ e
public final native void notify();- Y$ K7 }# f. w% O, O# }: m" L2 c0 Y9 m' f
public final native void notifyAll(); % Q& O9 p! e1 G$ d& p% Zpublic final native void wait(long timeout) throws InterruptedException; 9 }9 E/ d# Q% t; ~. v; W" @public final void wait(long timeout, int nanos) throws InterruptedException{}0 w( I' x7 F7 I5 D
public final void wait() throws InterruptedException {}* ~! \% y* ]3 T: h' h n
protected void finalize() throws Throwable {} // Deprecated' A9 [( _% @& A* {$ w) \
1* s# w, S( t. G% C3 v/ M
2 + L4 ~2 a3 ^! y3 8 A+ C9 Q( n- J9 m; w4' w5 L7 }/ t7 ]% s3 ~
5 , r) X5 m8 O5 `3 \# O5 v6 2 p* e; z+ Y1 w g/ L% L7- V' y) L9 g5 T6 B& b" t2 B" t
83 q, j# T6 a/ c
9' Y* p4 L6 \8 l: Q% P( W% W
10 5 S* d& I) |9 E2 m$ ~* F0 X5 _. `11! z$ S' v! r+ u% z! L/ f4 t
equals 方法 0 h$ i% `8 C: e1 W! M1 M4 T5 x7 ]
public boolean equals(Object obj) {& G6 u* d7 A- I3 g( B
return (this == obj); 3 P+ _, A9 ?- d' L( ?+ T% ]}' v3 o$ p7 X, W! `
1 ) N" u# T" L z0 o7 ~ D: E$ X; z2 # O* x! t, E e5 u) ?3 7 v' l8 G0 u9 n# d5 `7 n* _% L阅读源码可知,Object 中的 equals 方法是比较两个对象是否有相同的引用,但是这在有些情况下是没有意义的,更多的时候是想比较两个对象的状态是否相同。! A1 ~, X6 K5 [8 {
5 Y2 ?% M5 {/ r! q* ^* D% AThe equals method implements an equivalence relation on non-null object references:1 ^5 C* r% i; T& s" ^4 J( ?
It is reflexive: for any non-null reference value x, x.equals(x) should return true. / O9 D# q# [1 J7 L0 C- BIt 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. 8 L8 i& y' @5 ?1 P) n- iIt 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. 8 i. l# Q7 _/ F7 G. C# U0 _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.. M- E% A- r" A) q
For any non-null reference value x, x.equals(null) should return false.[2]! D) n5 v) I7 N* [
由 JDK 文档中可知,实现了 non-null 对象引用的相等关系的 equals 方法有五个特性:reflexive(自反性),symmetric(对称性),transitive(传递性),consistent(一致性),与 null 相比返回 false 8 S/ k2 T$ s1 S# x - A6 a0 j# w% |* Vpublic class Person { % e" z n# O8 g private String name;$ B, a; O2 P" c( a
private int age; - v9 @ d5 F8 W0 ~* ]$ w ' Q4 w5 j3 \% a- Q7 T' x; |* ` p @Override 7 L1 f4 a5 }! q0 s public boolean equals(Object o) { 8 n3 C" C$ P/ h+ M // a quick test to see if the objects are identical3 d) U J8 v- q6 g, S9 [
if (this == o) return true; : M1 v0 e# y1 p# j$ _ // if o == null or the classes don't match, they can't be equal 9 j6 C2 T- q0 C E7 C, J if (o == null || getClass() != o.getClass()) return false; b' w3 c& S+ A% M+ F9 B1 x5 j% _+ g& L' o% E0 J- {8 {4 \
// now we know o is a non-null Person+ h3 w6 _3 @% m
Person person = (Person) o;1 l& \* Q. q4 f* [# n
! O3 T T, T% \5 }7 `( l/ G // test whether the fields have identical values' |/ G4 D* n# s+ b8 y1 z2 p7 {
if (age != person.age) return false;1 y$ j* z, d5 h1 N; D
return Objects.equals(name, person.name); 0 }/ F8 n. n8 q# M* f5 g2 v$ ?& V }2 @: {! p/ {7 h. M/ E7 C
} 8 G! I( g0 G4 a- N1 9 e R q) l: Q3 i* z& d: d. H2; y; ]3 m! {/ k9 J u
3 % {% E! P* b2 t) I( O4 0 D$ B- a& g0 n5 \8 q7 q2 s2 d5 % U) h6 g8 g0 ~* `# w; T4 C6: B2 `& k, b0 [% c5 \! ^' J u* I
74 M0 u8 P& T2 K1 u( ?
8 9 r/ j, g# d& Q( q5 \% V5 z9 - x8 I- ^/ g2 S a9 s8 S. @106 D4 {" X1 `: A' ]
11 ; r y' d5 z# G) A6 S3 U4 J: ]! ]# [1 X12 . U* o: E2 m( I- z% M( U13 . C7 p C/ P+ F# t# i+ s141 T# K" [0 V! r+ z9 W
157 U! R7 u7 w7 L7 E& `" t- ]
16 3 s' P9 e# `' _1 |7 ~+ u17 / t9 v" T! V+ c% e! z18) v8 q7 e+ X3 H; j. v
19 + j5 l) [1 ~1 ~" R! v' ~- N0 E注意,比较可能为空的变量时可以使用Objects.equals()' o2 O4 f/ r; a8 F/ r L4 q$ }
2 v+ w$ U/ U, E8 s- T( y2 f
子类的 equals 方法: 3 e4 X5 m$ s* P& o+ {2 s$ C# S* J! R, A
public class Student extends Person {; r/ J" }! K1 E: D
private String school; & S% e& O8 E' L( ]1 [ A 5 o6 E+ c! k7 ], P( r- u$ T) J @Override v: `3 H* x% c. \# T public boolean equals(Object o) {2 U2 X, b4 ~0 Q
if (!super.equals(o)) return false;# ?% Q" J$ _1 x k/ j; s9 `
/ F9 e$ W3 P q) k/ d( T& x/ y Student student = (Student) o;1 ?) D8 u4 K% v6 |3 E7 M/ f
/ _+ ?1 k) r- i# e! }
return Objects.equals(school, student.school);$ f, q9 R; w( K; _: s
}# p. c- X5 y9 s
} D9 j9 O2 A$ y) D$ O' {* U3 v1 7 L/ C# ~; i5 U- H2. _: l. X! @3 a) C# V. a& ]& w
3 ' ?$ n+ J4 V2 X" F; F4! h5 x7 B3 @6 \' |4 t
5: C5 I8 G4 E% E8 ~6 A2 a
6: l6 j1 w H+ U! k. r2 \1 m# ^' [
70 ?" ?7 }5 u! n! S! e6 z; m
8 7 Y% x8 C0 M; Z: R9 ' i' t0 H) U" u/ s8 g* ~* _6 s10 5 P G" F6 J, q' I7 a11' X& P- Q- y) z8 w( M8 }* ^8 a' U/ i
12 & t+ j, k! f) I9 h8 H7 h我们在重写自己的 equals 方法时需要注意到自反性这个原则,即x.equals(y) == y.equals(x),在前面的代码中我们用到了 getClass 这个方法,在比较过程中,当发现 x 与 y 类不相等则返回 false, 在这里我们必须将 getClass 方法和 instanceof 方法进行比较。, G. R, ^5 C) I: b1 O8 b
5 [: f; J( D; ~$ L' G cclass Father { - E3 [( Q N. z. @( c# @1 \7 V& O( t8 {4 C
}4 W; x/ w6 C( c" Q$ z
8 ^, f. N$ A* y$ {3 `
class Son extends Father implements MyInterface{, ^, z0 I$ }& [/ K* R/ C
- \2 V" A, r) D7 m% S$ E4 Z! ^' J散列码(hash code)是由对象导出的一个整数值,散列码是没有规律的,不同的对象其散列码一般不同,8 Z9 k5 Z$ p8 ]$ M
& y& C* Q- _& d# d6 [! ~/ f |* O7 SThe general contract of hashCode is:& M1 f( e9 I0 D: p2 l
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.) c0 F) U) H4 x4 m6 H2 ?
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.6 W* q- l, O, ^( z: R! x/ `7 j* [ 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. * X$ \7 ~! f U' |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]; y$ }, z$ ?5 X
翻译: ( _3 x. l$ E+ ~2 E( J在一个 Java 程序的执行中,无论何时,hashcode 返回的值都应该是同一个 - M6 ? Z! h; Z# m7 B- \: q如果 a.equals(b) 为 true, 那么一定有 a.hashcode() == b.hashcode()) ?% g+ o6 ^% _# i/ I( {$ G
如果 a.equals(b) 为 false,那么 a.hashcode() 与b.hashcode()不一定不同 7 V- K4 R3 V: @" ^, Spublic native int hashCode();) B& F7 i u d! w% R1 Y+ i* i2 A
1 0 r& E* D& |2 s* A r以上为 Object 中的 hashcode方法,我们可以看到这是用 native 修饰的方法,表明该方法是使用了 JNI(Java Native Interface),使用了 C 或 C++ 进行实现。在实际情况下,大部分不同对象会 生成不同的 hashcode(通过将内存地址转换为整数) h) a8 G8 n- l* E5 q
$ k* k1 l2 y4 r- z; t! ]The main objectives of native keyword are:[5] 8 v' n5 w0 {) ^$ z# f: n( o+ W3 o: T) f5 R$ R) p! ~5 f7 C2 H
To improve performance of the system. / }3 b# X) q* k6 {To achieve machine level/memory level communication. n J% H, m! i4 c# ?4 g& J
To use already existing legacy non-java code.& l( s: O2 m9 u
测试代码:9 ~0 B D6 C- m/ N) ?7 _+ E
( O- E) ^3 f# E9 Fublic class HashCode {1 }% n- m- z. W( B# q* B$ c
; `9 j: n) e/ l3 n$ A! L0 J/ {
public static void main(String[] args) { ( A5 c, Y/ T f9 U% e Map<Student, String> map = new HashMap<>();, @$ k6 R1 w4 V. G; \2 o% D
Student s = new Student("Alice", 11);( Z6 v$ Z2 T# D3 G1 X# W$ T
' M+ s# a. L, d& K/ P+ w6 C
map.put(s, "Alice"); , m- C/ W3 `% h map.put(new Student("Bob", 12), "Bob");7 F; ^" I t5 t# W9 s2 L
map.put(new Student("Cici", 15), "Cici"); . Y( ~3 I% w) L6 A1 N" b( ~/ ~! n " V8 G1 r) G& @# c- D; Y* W if (map.containsKey(s)) { % B" o' o: u- C7 t System.out.println("bingo"); // bingo, T! P+ J( A4 s& V! s
} & J5 h8 w+ D) L% [4 x0 d, ^) ^- s/ z/ }
if (map.containsKey(new Student("Bob", 12))) {/ p! e) Z( M5 @2 {
System.out.println("hello"); // hello+ C- N- D; G" y5 O1 R8 D m4 T
}9 a! x) o3 C# Q4 I
} 6 I4 J. ?2 i: U, f- i& a4 p( `6 t- C, c( O" d5 W5 w+ l
} # b O6 B( A8 W$ u7 \9 L9 [$ N J6 }7 V9 f" @9 x- _: B& G" Zclass Student {2 H* k2 u! j2 E) V1 j8 \
private String name; + H" Y# D: q1 S7 C$ f( g! A5 V private int age;4 e E, W/ c* S
3 w7 f V# t9 k2 Q
public Student(String name, int age) { 8 H; u2 ]5 _ Y; m( m$ [0 y this.name = name; # J2 M) i- b& P% ^' {, a( j0 D this.age = age;; [: h9 y% s7 E& `! C) y
}% [ T3 s( r. a+ r6 |- v
. p: `, U7 I( ^# L$ ]* b
@Override/ ~" W; p; b) I4 k4 o2 I1 N/ R5 t
public boolean equals(Object o) {, ~3 J0 n# v+ v! r$ ]
if (this == o) return true; . c2 x- \9 T8 B; F0 | if (o == null || getClass() != o.getClass()) return false; & Q4 z8 {- ^4 n; {* y+ Z Student student = (Student) o;4 n% L5 D4 ^# o& x, O
return age == student.age && : b# R* D% T, [6 X% h. ? Objects.equals(name, student.name);, r8 h, x8 q+ W+ h3 ^/ q- ~, o7 N8 Q
} : W0 {3 ^- |- ^4 z- M# D& I' _9 U/ k0 V' T" K+ ~1 Q5 X6 }3 n
@Override+ r- `, g- i0 t$ E! H9 [. ~
public int hashCode() {+ T, Y5 m7 j6 C0 A" v7 G7 m) L
return Objects.hash(name, age);5 F8 p% ^; ]) o
} 4 f% X+ K g9 ]. E: \& I} ' r. s3 p5 G5 B9 g5 q1 : r( L5 K# S$ d8 s( I6 S23 R) X; G. e* q/ b* Z; r0 U+ z
3! Z, V. y, K7 z3 B
4 * E* I- a- c) k' _3 b4 E$ Q5 6 h+ F, T# ?6 v* h% ^) T+ s; f6) z% k1 b9 n, [0 t" f, X5 f6 }
77 d6 d' l) Y d2 V' h3 \& G8 N
8; e4 }/ I5 V5 `3 p; ~2 D; }
9 ! t F- A+ ~7 a4 y10 3 X% M0 P* F: C+ S" c5 ^, y11- c. ^" u% s3 J$ v5 `$ U
12 ; H" k$ j1 d: X9 z$ A$ M7 _13 / b6 b7 z# t8 l6 v, L143 T1 f+ G! d4 V9 E, V0 W, U
15 ' {1 |2 `, s) S3 ], w) h16 6 G- j/ L% p0 `# Q6 L3 m0 ?173 ?& v: ]+ R9 F3 K) E7 j
18- `) `1 C! d' }* j$ }+ I
192 ~* r- q c0 H) [+ W% O) I; R% h
20 , E0 ]7 ? D; V. }" \" A21 , e( w/ y1 n! p0 ?! j9 g1 Y. {22 ( S. C5 h& D( [. q23 ' K, t$ y5 s/ I! t+ ?24 7 m2 P3 x) P9 V% Y252 o, ^: o; I5 b U
26 " Y7 U/ {3 @' A2 |% a/ T27 2 g b. `# f: ?& k# _* x7 \288 E' w+ w5 I1 @+ k0 C4 B- X
29: V, D2 Y) y/ ]% w5 K4 J* \: C
30 1 l3 c. W) p3 X6 t31 6 r( Q! G) y H4 g9 V$ z32& i& r$ J3 r `- A2 c
33 ' ~9 q: _' _1 W Z( q# G4 P341 [. w8 x+ |2 k: x
35 5 A! T$ }0 b* X; v n* E' X36 $ b+ n% o1 I* j8 x- L0 a8 K% m377 e9 c6 a% _1 l" {3 d+ q% Q
38 2 O4 J% Y& \2 f/ f396 u1 Y$ V6 \* T4 }! X: G9 s% h) t
407 `/ L. D. A- K# [( J6 q9 U
41 6 n0 _; w! P( K* ^# p. ?9 K42 " Z3 Y+ |. }" K; |9 H1 ~43 * u7 h, K- T) w ~$ |2 G: d& N/ b* X y44 ( \9 G6 q- X3 U9 S上述代码测试了,必须重写 equals 和 hashcode 方法,才能正常作为 map 的 key,如果有其中一个方法没有重写,都会造成 “hello” 不打印。 % _% r9 |1 v( r2 u# v0 X7 q% Q- q( E2 T J$ L5 }3 g
clone 方法 , ~3 h4 y4 P2 U9 Y" j N2 O- b9 ~. w
protected native Object clone() throws CloneNotSupportedException; # T; j6 H% H _9 Y7 H* a1' e: ~/ k" S; Q3 `" A9 |
观察源码可知,clone 方法是 protected,子类只能调用受保护的 clone 方法来克隆它自己的对象,即只有在 Person 类中才可以克隆 Person 对象 $ T8 f0 i- b3 z% s2 h3 T# R) F7 V 6 k- h% M, W% S4 wpublic class CloneTest { ! E4 Z6 @8 c8 ]8 x! V: H public static void main(String[] args) {. _& Y% D$ O( D# |) \+ f% ^
Person p = new Person();5 i) |# N( Q9 Q5 U- t5 X6 z% B
Person p2 = p.clone(); // Error clone() has protected access in Object3 a! v0 k. M; b: R1 g$ U2 Z4 x
} : M7 c) q6 l6 h, S; g$ a} 7 C. Q. u. z5 V9 ]6 B 8 D% f4 x; B0 s0 t/ Lclass Person { : p7 E" c# b) M* s* B }5 Y- p7 | private String name; , K; G( W1 W; J3 }8 x private int age; $ _. z1 S6 K& P6 p9 ?3 }; g} 7 z" g s8 u$ b5 q4 k- L1 _* _ ~' _2 h! x/ G2 ( a1 D/ b/ H. J0 p o# ^* M3+ X9 K8 t/ W0 c6 E. ^+ ~5 W
40 C9 V. J4 P7 e0 @' i: [
5# o: J$ g: s8 p" {. h- D
6( ^7 h: T* y8 d" W# _- U! i
7 $ d4 _0 s$ @1 a& Z8+ \2 i( B$ r- i
9 % V/ o6 ^$ g6 P0 H( @10 3 e* w$ j4 B9 n+ J11 3 q% T5 @# [9 D& x) ~' ~我们可以重写 Person 中的 clone()方法为 public 即可调用 ( g4 w& Y+ x7 H" ?, n6 H# M+ J& j6 |) Y
public class CloneTest {( z7 k5 `" f; t$ e5 G, N
public static void main(String[] args) { . b W' J. E- g. `& B" F Person p = new Person();4 |1 D u1 q3 V6 o' y' G
try { ! S' q% q; k5 |9 a7 N! N- S. E Person p2 = p.clone();0 p. H7 \3 \" p1 r6 T9 [$ W
} catch (CloneNotSupportedException e) { $ s3 g; I( n8 N! ^3 s e.printStackTrace();# {! S; u. v1 E5 }2 L' q
} ; n Z/ w; `) B9 j j9 f }' c9 r. R9 T! v( g) r' Z9 I
} , k, B4 A3 r6 `1 `$ Z1 a' R! T- C- i( Q/ _8 ~
class Person {! p; y0 S% `& X, F; x
private String name; V) m( o3 n. v: B5 n private int age;3 b3 }8 B$ i D; s
3 w, U8 D& e7 B8 `/ Z @Override 7 C( Y- q2 v. k4 z protected Person clone() throws CloneNotSupportedException { B! _" t9 D* G$ d0 p return (Person) super.clone();4 _2 r% p( B2 |
}# z2 B0 G y$ U* I" w
} ' O9 t9 L' \; |* }) O1, c$ Z2 M, z/ G
2/ R, u; f+ S" w# P1 q# V
3 * z& u7 E7 k+ a% z/ ?9 j4 3 W2 G; f/ x$ n7 l$ c C5 9 B( i$ B- h. E( [2 S; Q2 v. b6; D l, |6 s- d7 b7 X) ~) A, |
7 ) t( `2 f- Z. X/ N( G {, z* @. _8 $ ]/ ~. I1 E, l1 u9 2 q" Q, n2 M. _2 W; @10 ; G, f) {, @7 ~; [+ b/ L11# y" f u$ k! C$ y$ U. b/ N" g
12 / J2 j& o; f% z: X: d13 + N$ K" p( w, F# a. r' l14! z0 i7 n1 E9 y& x* x
15 # K" ?) N+ P' E6 ?8 M16( i3 b: b5 N6 A6 e$ g
17 - ^" J! d1 i# N& J1 I9 j% X: z189 q1 _: H" c" T5 K6 U, a, p% @+ _
19 ) {& ?% b' y% I7 |2 s20 ( E: w. U8 {6 J. t I: E0 C0 s但是当我们执行代码时,还是会抛出运行时异常:java.lang.CloneNotSupportedException,这是因为我们没有实现 Cloneable 接口,这个接口是一个标记接口(marker interface),只是作者了解克隆过程。 5 e! b* |5 ~7 ^/ f2 y4 Q' v. I b* v, z) h
浅拷贝 8 t: @5 j5 {4 [+ Z! J' G8 F. V1 r/ r
使用默认的方法得到的拷贝为浅拷贝,即拷贝的对象与原对象中的引用变量指向的是同一个1 n- l5 U; k$ w8 o* C) w3 {
" T( w% i: @* \$ }, b" P
public class CloneTest { 7 W+ n. v7 g. \# @/ r T. T public static void main(String[] args) {. j$ R b3 f2 o. [8 I6 k; ^4 q
Person p = new Person();) w5 p0 b+ j7 ]/ v
try {- Y- @; e f ]- C% V/ h9 W
Person p2 = p.clone(); ) E$ {* m3 _5 V# c5 l System.out.println(p.get(0)); 6 I8 z+ c2 H0 g0 x9 C0 g: M, P System.out.println(p2.get(0)); 2 o4 f: m- S. K( m- S2 a" Z2 r p2.set(0, 8);6 ^! p) s$ l8 h# X0 h5 {- D# ?/ V7 Q
System.out.println(p.get(0)); ) ]4 y% o3 H; @" w/ W+ I System.out.println(p2.get(0));- a& Y9 R, ~! W& e# H
} catch (CloneNotSupportedException e) {$ ^& J$ N# G5 o2 }; Z1 Q
e.printStackTrace();- |% i% z' L% H3 q
}) L+ }4 M% G6 X4 c( l0 H1 G
}. T2 v; A; E& ~# R+ t# z3 w
} . ]( t8 b S0 M2 s/ d& c+ d, S4 }" `8 t$ P1 L) Q$ D: ]" E: J
class Person implements Cloneable{ 2 ]: `2 R/ u) s" R E i" a private String name; + @9 {3 @( s8 d2 Q5 s b private int[] nums;: @; Q5 H8 Y3 l8 N
% K, p' T* \' Z9 l! Q8 @
public Person() {8 m: L0 |9 {9 W5 U: j
this.nums = new int[]{1, 2, 3}; ! N9 v9 s! N/ t" i } 9 ?' {7 r6 r2 m; {+ u& \9 d5 n8 n! q2 M; I2 L1 K! u$ S
public void set(int index, int value) { : i, Y. [2 n Q/ i" z this.nums[index] = value;7 Q) g& w2 r, q$ D6 N& n
} - M. M! Y6 z6 z+ x" f3 q; `4 A& T4 L% w( Y' H0 r
public int get(int index) {. m5 N, n* Y- o( P- l/ P% z
return this.nums[index];6 v8 S" ]& K6 _# }9 `& a1 T3 I& G
} 6 m5 L9 N7 I0 D! B( J. |$ b, {* M& F! {
@Override 9 E3 F/ i+ l4 j! Q7 A protected Person clone() throws CloneNotSupportedException { & o6 ^8 b* k7 r return (Person) super.clone();) d# C+ f3 W0 w* h
}: H$ T8 ]7 M5 |7 d' ?2 k
} * {! d. B+ d- T M7 T3 `9 A1 u. u% n
// output: S5 `4 H, e6 Q7 S) H, P
// 1 # E7 T3 K1 _, P9 u+ W# w$ ]// 1. r9 B/ Q4 c u4 D( R* X, c( d
// 8 $ c& G$ r8 i7 Y+ V// 8 5 w$ b F. ]% `+ w; ^/ c1 4 R1 c/ H; ^% u# g2 H( b8 d4 r25 k z l6 g9 _- X
3 . K3 P2 [4 A* v4 $ Y7 G9 ~1 G9 \3 @0 J5 v56 l/ q2 Y+ V& S1 g6 ]
6% ?9 c0 o1 a) d( Z' n8 a3 ~. m( F. ?
72 ]6 F7 k' Y- }/ y" y0 G3 b
8& X, \) @1 D3 @4 e( t7 c
96 I- Q6 d8 r0 x# `$ A9 e% A
10 0 S5 w2 r& G/ h5 T8 [/ g% h11 # Y! j/ s3 `$ s0 W+ e. m- _12' O$ D/ q" p9 K/ d( Q% N
13 9 F, f; M. Z2 {, B1 Q' }14 ! V% b5 d' U8 D* l4 S$ e, l' g7 s15 # G) r4 v$ i% t1 F' r7 H2 z% S166 H6 j5 |2 K- M; P% I5 A
17 $ O. Q' b( ?0 W18 ( P% V+ [! F2 s, t% W' X7 h: Q. S19/ ]) n! e5 x/ ]& a- W
208 P: ^( O- b( Y/ K; t! C8 x
21 & f# \9 j6 I, P$ O2 r: h22 5 }6 }4 L* d! w5 S7 {( x23 8 t5 ?; k, u9 n- _2 m6 j24 a; N) U' }. l* e, n, K% w. n253 \/ ^' M) i2 v
26 Y# E- n* v3 g0 k# \3 I9 ~% h6 L27; w- v( m; M$ g L
280 r/ n* h1 _8 M( o, u& r) s9 V
29* y; y0 j: Q# A& A
30 8 ^. z7 x: V- p; b8 y31 " \8 q2 y9 O7 a# w+ Y5 {1 C5 u# W32+ C' v& b% p1 Z9 b
33) X1 O/ z v' |6 h. K2 W# a" Q2 w
34" z, l5 |9 ], N$ n4 w5 Z7 w# e
35 / E' ?4 k, _ k) G: I/ W% G36 ) n* ?6 R9 D+ F0 g: u3 r3 x8 R37 # `0 x3 ~" x t4 b% }; J" N381 b: I# K4 X' V! b! ~$ Z
39 ; v7 b: |. c7 {, a40* S) h9 a7 j( A1 R4 R. ?
41 0 x( ]5 z0 s9 E! l# b& E5 t427 j/ M9 {0 I, f4 s+ S8 \
43 ) d, h" J0 o, d7 f; j* O9 K' J深拷贝 2 z3 C1 C, I0 V6 _. y8 W H4 v ( v$ p8 `% x0 {为来解决上述问题,我们可以使用深拷贝 : t5 D! e+ O B+ B- C) V$ ~" i. k . i _! W9 a" `public class CloneTest { 8 a& Q- u. p& p public static void main(String[] args) {( _# i4 Y( c9 r- q
Person p = new Person(); d6 B3 ?5 s/ ~2 P, r% V
try {! ~" x" `7 i' r: ?; {
Person p2 = p.clone(); 5 Q, Y8 m; A/ l' u9 p System.out.println(p.get(0)); 7 b& O! ?4 t @. C( n System.out.println(p2.get(0)); ! x$ g3 \! [8 a$ m p2.set(0, 8); . k4 \8 }/ f7 p System.out.println(p.get(0));9 p0 [& |, i+ I& x+ D
System.out.println(p2.get(0)); ) Y2 F, y( u) W8 z2 `7 N' N } catch (CloneNotSupportedException e) { 3 X H$ k l1 R e.printStackTrace(); # K! Q' `9 X9 F! P3 F: a3 M }/ }: l+ D0 {$ w( n! ]$ y O
} 6 [0 |9 w) G( s l- R} " G P# h& [. r- G: c0 f , O( t, F/ a0 p; r6 \! lclass Person implements Cloneable{ 9 G+ X! ]( P ~9 P- U! O private String name;# @2 m/ O; R8 _' S
private int[] nums; 8 B% x# K+ P- t" u% D7 Q2 G4 A8 e, Q9 h3 L/ z
public Person() { ; M; c1 H4 U- V; }* a this.nums = new int[]{1, 2, 3};: N9 ^6 _5 x( ^7 g
} 6 T" Z3 t# U# K, _) L ) g/ `( S. l. A2 @% k public void set(int index, int value) {6 d* @1 O7 X, G1 b$ _" V9 X
this.nums[index] = value; % H5 F: u1 R3 }- a7 t } , K. T* Y/ H( |% F& [9 }& _+ [9 }$ x* \+ }# a7 H# p
public int get(int index) {' g8 `' ^+ e) @
return this.nums[index];* ]7 i' j6 N/ `% P
}: G- ~) m* C2 k/ H
! n2 _! O8 |3 a) ]: ] @Override- N) t2 x0 n/ a6 i, B5 l3 A
protected Person clone() throws CloneNotSupportedException {1 |2 A" i4 Z5 b* w A+ X
Person p = (Person) super.clone(); : i' E! l1 M; `1 G* P# y3 V p.nums = p.nums.clone(); // array clone : x3 ~. V' c/ `0 O- p% w5 | return p; : `6 r; p. L% b; `. p% p }" T( A$ B; h6 Z7 S
}5 j3 W( v, S" W+ V; u* r0 _
* m& E% g" B! B# p( {; M! B! q// output , B$ T1 ?% d6 m; j' c// 11 c3 p7 f( O8 E& S2 w
// 1 V& M3 ~/ ?, L' x+ z// 1 T$ M5 t, I2 }" R. R1 j& B* H
// 87 @) {! _; I4 U. c
1 ; t4 W+ q* G0 ]0 a5 q8 a4 h- k2 * p# m2 \4 ?. W u3 3 O( x7 P9 F9 c5 s4 4 o, M+ ~7 |9 c/ W5 j t5& p" {4 F- C% |) I2 ?
69 [8 T- x. j8 |) d( F6 k
7 - @% b+ p/ q+ N: H$ W7 l% Y8 ) M/ e. P* \ k4 i* f9 ]4 r# b* d* i/ a
106 d7 g5 X, J: B" U) m. ?8 Z* m8 T
11 ( H% e3 K1 Z. I: B F5 O12 2 c0 N+ u+ U6 @2 B13- q1 c: P; r" [3 ?' X4 T7 M% [, {
14 0 R' X& \* F! _# o- i1 T. T15 4 ^) T+ K% c/ q16/ M, i9 ]+ k w' D1 w9 D- e
17 1 X' G. K# b3 _3 R# B18# |' G0 A7 _9 C, Q9 G
192 J: P7 `. s6 i6 I. v* m. F
20 ) W7 S Z5 b* l/ G21 9 g( q* \3 h: ?) \/ S* y22 + n, K5 n: ^" U8 U8 F( _0 \3 N23% X2 x) ^$ ~- L) c
24 7 m0 z* `9 u. S# T% R' m' |253 V8 ]8 f3 ?# m/ J' E7 w7 o
26 ; U9 N' ^+ e- x/ p& `# N3 ^4 U* V# @9 U27; W- \) ?1 `; `; _# M: W. p Z# K
288 G* H7 e- O \* B3 Q
29; s* g3 o7 k) d$ g) v# J" p
30 ! u2 _, ~0 o& Z) k) n' \; k3 T, A31 % ^9 `5 d. o! D- ~9 l6 b323 @" I5 k, E5 @" S* j
33 ( J' l- |- e, u3 v4 a' g34 3 Y, L' Y$ n9 ^9 V" R1 J+ K35) ~7 I3 ?. j3 n, u
36 2 d* R( O8 _0 i37: I- ~: h* t4 W# k1 P
38 # U* H8 W- g# J( ^39 ; Q" Q- k+ k4 X& J7 [' u% f9 z' y+ d40 $ Q5 p. N) W3 s" m/ L/ y l; `- W414 M6 W2 s1 v3 S3 ]/ J
42 $ ^& X) H: i. r2 F2 _& K43 " R: f5 V# G& }- v k7 [( U# q44( d, A2 \% W: ^ a
45 : w$ q& J: B k1 |注意,数组的拷贝可以使用数组自带的 clone() 方法,该方法为 public,所以可以直接使用。若有其他引用类型变量需要拷贝,可以使用该类型的 clone 方法。 3 Y; f; ?6 [; \9 t- W) r! m2 Q + p' H4 v7 V" pclone() 替代方案 - R7 X/ d4 Z7 c 9 U: q$ n# t+ p( H2 Yclone 使用频率一般较低,我们可以使用拷贝构造函数或者拷贝工程来拷贝一个对象:; B1 N8 y5 `) w- Z0 k
# E" d5 ]& d$ `1 z
public class CloneTest { ! M2 J7 m. O6 v9 s- _# L) F) B public static void main(String[] args) {* ^1 G* v3 T$ B4 Y0 q9 [7 \5 \
Apple apple = new Apple();/ L/ e0 r* O1 X( N. V! N
Apple apple2 = new Apple(apple); , n9 {# `# ?) u0 Y System.out.println(apple.get(0)); 3 L& g- [6 v) i3 I4 K System.out.println(apple2.get(0));) B1 ]5 }( s4 b$ L$ I
apple2.set(0, 8);3 V) N; @ W! x ?' T% Z
System.out.println(apple.get(0));% ^3 |* s& l7 D
System.out.println(apple2.get(0));/ }3 o4 \/ j" f' R8 I$ |
} 2 ^. q5 L; Y9 t2 U* m4 [2 q2 D}9 M: }- u6 y o( [4 J$ J/ v% f
class Apple { ( o6 S' ~# \! m/ X- H, H private int[] nums; 9 o- E5 ]- \7 R1 k private String name; 1 `4 |5 k# E; [; w x) J0 V7 F8 J( i: b7 i, W1 F
public Apple() {! `: ?9 s. e: ?- G* }
this.nums = new int[]{1, 2, 3}; 7 B+ S* D1 [) Y ]! D) }1 m }1 P# V) U8 X0 |( H. e3 ]( k0 m
# \. |0 v) t4 I
public Apple(Apple apple) {# }# r [; a" w) \( K
nums = apple.nums.clone(); * ~0 a; ^4 ~* T4 z. T name = apple.name; : [5 \; {, }7 q; ]7 H' w# _/ B }1 A1 Z; O/ o4 H7 E" z0 G* X+ G
1 e4 ~; g5 Z2 f$ I- I1 R4 U+ o0 J: ~4 X public void set(int index, int value) { / b. f1 Z( [7 \$ _ this.nums[index] = value; ; S* R7 e c$ j0 B } - a2 {1 P0 `. L6 O3 O - R4 I% M$ u- W. l public int get(int index) { |' i& f T1 E+ s return this.nums[index];* N8 V4 c2 i; T" l' Q2 a( D- g2 [
} 5 j2 M7 k. n( C6 s- ~9 y4 `} ( ? N ]1 u7 u f) \1 l% G- Y* a! e! I( _
// output" \9 M1 {* n* _! ^) t; d7 z
// 1 ! e* Q& P# y* U# i# }/ e. n$ w4 v// 18 O4 I) ^: c! m' M
// 1 ( i0 b, g$ X) M! m* }5 u// 8* k q) ?$ [- Y. @; p
13 _! Q$ P& k; G7 q$ w
2' p Z" `0 S9 N6 {( e. ]
3 P3 A) [: v& I5 u* a
4 7 m3 M3 l6 P+ ~6 I2 \3 t' u5 , a+ m3 ]& n2 a2 J ^) S: c6' L/ ^& i8 _. z1 O+ g
7! K6 ^* B0 H& f8 g# K
8 2 ~; I% L6 ?( J4 N: P3 ^! g, y0 R9 & y( ]- @/ ^5 o) o10 : X5 G9 o: ?# a4 u' [11 # U! c; u" b6 K4 J8 F12- b$ C `: {3 I
13 " t2 [5 E: k/ i; }3 n' K142 v7 L7 d& V: D
153 Z) `9 D3 l; F
162 P* ^2 z5 a# k6 g
170 i( A& N% n% U% ~3 C) H5 N
18 5 B& ?( z- i$ f# F19, ?# u9 F- V, m# Y
20 ! y! U# {1 ^6 y$ Q6 k21 & d j9 P" c0 |# D22 1 ^, ~3 I. V- l. p( p8 v; [! ^23 : q7 a7 @3 X! M: x8 T/ D- K241 c6 Y/ I0 y" w/ T2 x# v
25 , U- b' [6 i+ \5 Y# |7 G# B2 W26 # ]2 ]2 }$ {' l+ N* i( i' C' V1 E1 T27 4 M8 f- n, ^0 s2 g+ `28& W; ^! {) t/ d! W! N5 G
29 8 v. }/ v: C, l8 z( O30 ( H. }* v; S: }% J: w, P31 7 k$ m: k/ k6 j& p3 W$ a2 L32' ?/ P: v0 |, c& i
33 1 @% B Z$ F( y34+ n+ g9 @0 H4 a; x3 k; D) h9 H T
356 A4 O. o( @+ b7 E# C" T6 v
36 0 q8 T& K+ j. l2 c0 ]370 N9 G, f, [6 X9 q9 v
38 # h5 B" Q2 C% i* T x9 R* A8 aReferencee D0 T: L D& O8 o4 ], U5 P* d
9 A9 r% i4 t8 `' ~
Java核心技术卷一 $ x! w+ R9 R& U C0 T2 Z* W1 @# s( p1 k2 m8 B* P5 n8 ]: i
Class Object % w9 O3 p+ W# I6 N# Y0 x 6 H7 j1 s% Z% B2 Y9 w" C: zEquality, Relational, and Conditional Operators( A* F/ u4 |/ p2 n( U6 h
& F ~( v! Q+ g! R$ K- D* z
native keyword in java; ?% a s" ?* h
————————————————( P9 g* I: |# |
版权声明:本文为CSDN博主「EJoft」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 ; j9 c; }% o& n" V: ]& X原文链接:https://blog.csdn.net/EJoft/article/details/106012278 1 m6 [9 e6 C1 F) s: N! {* g& ~
% e/ x& i6 ?- \# q