4 z* f$ d/ U( K3 ?( [% S3 JThe 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]1 b3 [% g7 Z) s, N$ W6 B
简单来说,instanceof 是用来将对象与类型进行比较,我们可以通过它确定这个对象是否是一个类,一个子类或者一个接口实现类的实例。# X/ p1 Y7 z' V
1 X/ f9 `. d4 |% F7 D* r# h6 Qpublic class InstanceofTest {- ]& E( z2 E! g$ K6 E0 d- {8 z& W/ ^
public static void main(String[] args) { ' \- m$ r# Q A# r2 C9 U7 R Father father = new Father();8 w j: `( c4 `: d% R3 r. `8 e
System.out.println(father instanceof Father); // true4 T0 q& U- U9 C! h
System.out.println(father instanceof Son); // false 0 a3 t. P& n2 ~; X System.out.println(father instanceof MyInterface); // false' K6 ?4 a) B; Y( S4 ~2 n
System.out.println("------------------------"); ) o& j, x& e9 O1 `& V* d. @# u3 [$ Z- J
Son son = new Son();/ ]0 N4 j; k+ u) h+ r$ t
System.out.println(son instanceof Father); // true _: i+ Y! X: V( g& r- x
System.out.println(son instanceof Son); // true ' N7 G+ E0 D* m System.out.println(son instanceof MyInterface); // true & c$ [7 z i: u- O4 {2 U System.out.println("------------------------");8 A$ t" ~% @% n6 D) e
' C7 c0 H3 i$ O$ ?( K3 U# ?
Father father2 = new Son();& y! o/ G: T( n* w, K
System.out.println(father2 instanceof Father); // true 5 @& s+ D. g& S) G) f | U& e4 \ System.out.println(father2 instanceof Son); // true. O2 N1 j+ |% V( W2 c) T7 \( I
System.out.println(father2 instanceof MyInterface); // true2 r1 n7 |! |5 q4 O. U8 r
System.out.println("------------------------");0 {- J2 @' i" w+ @9 D
}0 {) u6 I$ O" E) j* B3 ~
} # S4 V! J: J, n; r5 F e- Nclass Father { 2 F/ f; T) o' d4 ` 4 O$ g" d! ?$ z2 _}2 u! S7 t1 ?- [( T) Z6 z
9 q9 H7 ~ n6 O: E3 R/ G! N9 y. U2 y+ Kclass Son extends Father implements MyInterface{ 6 m* Y- f ?) v- ]& ?3 e! `. ^7 z0 x0 F- H$ E5 S" ^ Y
} % V }/ G$ v4 ^4 B- ? / D, [: F+ b d% Q# e' O; Yinterface MyInterface {7 Q4 _% l! M$ N/ Q
, v0 c) }, p; s5 D1 S3 t0 J
} $ `' J: }+ g# ?/ a2 K1 R7 x1+ q5 I0 }) Q3 B% f1 o. ^4 y% g# j
2 ; G$ L& d7 V& {- o0 C+ B3 * d1 _, N( C2 Z4) C: |3 e, H2 t
5 5 F) f, [; r0 {2 n6- z! _2 |. L- b+ C
7 + W2 [3 l8 s9 B; E/ d# y80 w" F% ~- ?, y' m7 T6 U
9% A5 u$ `4 V" d: ?; g
10, f% r4 A2 t2 \7 [
11 - @# F- P1 z2 Y/ G12: |1 Z# j, ?% c h9 N; b
13! B& L$ \+ _9 l' B4 N
14: r$ |! E+ d! o8 q2 X# D/ f
15 ) V( `4 @% ?" X: A, U) C16 ( t/ L7 v; g& k* ?17* Q& ]& a( k. x9 i m
18; y- b, S& A8 A
195 q7 y8 z% u3 w }0 D" a h
20( o' J, M m- T# `
21 3 Q' M3 y9 z6 A- Q0 `227 B( s! P$ q' _9 V; Y$ x( B
23% q [4 a) U9 P' ?2 \3 t
24 1 \1 H; {3 e9 n; R3 T, ?. R/ n25 7 w, y% Z- F- j; J4 H26- `! M1 m; K4 K( u+ u, x
272 A i" ]5 ?" N8 j+ a# H2 M* i
28/ N7 [. S: m7 L' d
29: o$ O+ b a( U' H- T
302 }" k& _7 _1 Z' I5 r9 Y+ t0 T
317 _* t7 |- l, l. q
32" T, b* L+ |" N( `9 ?9 Y
上述结果我们可以看出当父类变量被赋予子类对象引用时,其结果和子类变量是一样的。( c4 U2 W- ^1 B5 n7 Y' f6 j
. U9 W! a2 J b a v# z
getClass( z' F8 v1 O( g; ^$ E
! `! m8 e- s* A2 |3 g$ H' h. \ public final Class<?> getClass() ! b/ x. \" d+ R$ M, @0 D2 P# p1 8 _( B2 M+ f( M# R8 E, \4 w, i" IReturns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.2 h+ f& I5 ` T
public class InstanceofTest {+ w% z0 t7 _$ t, F: [
public static void main(String[] args) {1 Z+ N2 m$ _9 E0 p7 z
Father father = new Father();! L. x$ L* T0 l7 f" u" r" J
System.out.println(father.getClass()); // class Father C+ C9 n( A! s* l- K: A7 m
Son son = new Son();' }* R# F& r- j6 E
father = new Son();9 R3 Q3 a- B1 R5 u/ L
System.out.println(son.getClass()); // class Son* l8 O* d6 p; a: A- A8 N
System.out.println(father.getClass()); // class Son% x& q0 W3 t( ]* x( T
; g. h% q! n# P }- H+ |% K& n7 ?4 N5 L' ]
} 1 c8 S) P) p& G* u) q$ q) l: H7 x6 i |: K- } \( k& b3 L zclass Father { * Z9 a8 z$ I- O - v3 L% T3 F' ?; T0 |} ) p" J* ~, p6 L$ l' W& y; V+ r' x' q/ r4 G; t2 g" j
class Son extends Father implements MyInterface{ , q; [ v. n" a 9 I2 O7 }% m5 K# ~7 P& W} $ e3 P) J. P; I8 @ 4 m/ n7 u/ E3 l6 a8 z0 w) dinterface MyInterface {4 w" R( g( G2 {1 W4 u
9 V i+ ~ {0 ^} 4 s+ p$ h% [0 l$ o9 d11 q& f% {2 ?; D. }# n* B
21 s `! @: T5 j* w. T9 R8 S3 X4 n
3 4 X; i8 f7 z" |; o8 [4+ R/ m3 L. N- P* U
5- s* h3 \' G3 Z; M
6; s% H0 w! f8 D; A* W4 G; w
7 8 q7 H! P4 g9 u/ [8 : W' p9 {; G. K9 d) ]5 @9 ! p" m& X4 h) Z+ ]106 A5 d* Q8 y7 F$ i8 L- O
11) c0 M7 p4 p; F$ |3 f) v: d1 {
12! l7 {$ L' ^3 M7 e" I8 u
13$ t0 h: U, _' L- O* e0 ~/ O! P; |
14$ M3 o, O% H; X6 Q
15 9 M; C0 \5 j2 [4 i" M4 Y( |$ m16 & n. `* Q Z& q, ?2 \ h' L17 - i8 v8 Y7 D( I18 $ A, c, n: H% u) L; O- I- W! j19 $ k% S2 `& s* t" @) v/ i+ k20) R) D" q0 D0 }# I8 y
21( h& @# S, r- n0 @# Q" k3 M' E% V
22 , E- Y; u/ A6 }. A23* l, Y" p( y* y
观察上述代码我们可以发现,getClass所返回的值与对象变量此刻的对象引用有关。 & M4 u1 z0 V3 q5 N7 F9 h7 {! ~& ?5 }. P; W4 f5 s7 M- U& {+ N
相等测试与继承0 K: y% r' t( G& Y% a- X+ p/ J4 g
# O9 F4 |8 \( b) A( h
我们来看,如果复习的 equals 方法中使用 instanceof 来进行相等判断,即使用以下代码片段, c6 I+ H8 p- e9 Z3 M/ b8 _
) `7 V0 ?/ b6 l* K, B) Y
class Father {0 N$ \+ P0 z* z5 a
private String name;1 q: z3 P9 @- ?+ H- B7 b1 |
4 t& {$ q0 P" ] @Override - g9 Z! I o$ Z1 w public boolean equals(Object o) {# O, Y* B4 A- t3 J7 C8 T! U% U
if (this == o) return true;) H8 O- _' y* b4 j: P* E6 }
if (o == null || o instanceof Father) return false; 0 f! X9 I* X' G ^. Y8 ~& Y1 A Father father = (Father) o; 6 {6 ?9 h* P) ~ return Objects.equals(name, father.name); : h5 ^# j7 f! v4 w0 e# B1 h }$ g8 Q. K% ]; ?
}1 s1 B5 E; ~- u4 G- I5 q* X
* I, C2 |+ q3 {' a6 Q) d) Sclass Son extends Father implements MyInterface{ & a* i. S4 v0 n, U. w) p2 S private int age; . H3 d2 m8 G# @4 M; I$ q* ~3 a8 ~0 O( s4 |- }9 q3 `# [& N& g
@Override % W, ~! U( C3 g% \) u! _2 W1 Q public boolean equals(Object o) { ( H6 M1 W3 W% N# B1 [) s! [ if (this == o) return true; * e8 U/ r1 `0 q V- u& h5 y; I if (o == null || o instanceof Son) return false; // not good & s3 U: X2 l h; F if (!super.equals(o)) return false; & q8 b3 ]; C7 P t0 e1 q Son son = (Son) o;1 L' j: N! j4 o: _) t8 U }8 q
return age == son.age;& ~: U% g- H* a/ B
}0 ^: Q/ `4 f# q" O/ }
} , b' g: Q- e- }; W8 U2 L4 g7 Z1 - d& c, p. f5 F: j- _! r& C0 r$ i9 g20 O1 Z- Y5 {; c6 ]0 ^# e
3+ J# k4 d' \. E! @4 W
4, u( ], M, d3 B
5 7 G" v v5 o, r6 4 y& O2 }6 J! }$ u+ {; i# ^7- R+ x2 C) \$ G. ]
82 b3 I# {% e6 \ I
9 & d, Z5 L) x$ P3 t& I) a10 ) ?5 M# [$ j) P11 K. L0 e: }6 s; n12; f# M3 A4 V3 H9 v) p$ p. e
13 a. O8 q; L) r! t Q8 ^- o/ A149 `' ^8 ]! @6 b
15 " R& r- V' D8 c, N% S1 I16' k3 Y5 U9 t( v3 ^
177 K& V4 r! V; h1 P" n( R/ F8 ]4 _
18* ]# M8 h$ a9 Q, p) n4 l3 k
193 H5 c1 r w5 }5 v& L0 s2 V/ S
20- Q4 o5 V5 w% S+ v( y
211 ~9 t$ _: W5 y$ y" m
22) P/ W# p. g2 \. {: J5 g) `0 _
23 9 X8 F3 U x5 l* P24 4 T$ \# H) {9 ~7 d( B- ]+ ?当运行 f.equals(s) 判断时(f 为 Father 对象, s 为 Son 对象,假设两个对象中 name 一致),根据代码会返回 true, 但是当运行s.equals(f) 会引用 instanceof 判断失败而返回 false, 这就不符合自反性。 6 e' Y1 e1 C! m6 f. t6 f5 K v& {1 e* t
综上,我们可以得出结论 ' O- D/ b% E5 t' |- O( v! ]1 w* e" y7 k$ }8 T8 P
如果子类有自己相等的概念,如 Son 对象还需要比较彼此 age 是否相等,则对程序需求将强制采用 getClass 进行相等性检测。 6 R* K5 j4 D' u G0 |/ J如果有超类绝对相等的概念,那么可以用 instanceof 进行检测,只需要超类提供 equals 方法,并且要标为 final, 子类全部继承超类的 equals 方法,这样不同子类之间也可以进行比较。 ( _ ~' L; f' O" |4 j复写 equals 方法的步骤: 7 f; @) X& t7 ~4 R& W. c+ u( t$ D0 [1 {1 y+ E" h
现实参数命名为 otherObject* L# ~6 o* I E
检测 this 和otherObject 是否引用同一个对象: if (this == otherObject) return true; ! m; l u( D$ [5 B8 b检测 otherObject 是否为 null: if (otherObject == null) return false; / j3 e' ^) h& e* ~比较 otherObject 与 this 是否时同一类,如果 equals 语义在每个子类中有所改变,就使用 getClass 进行检测: if (getClass() != otherObject.getClass()) return false;如果所有的子类都有统一的语义,就使用 instanceof 检测: if (!(otherObject instanceof ClassName)) return false; - z3 |" C$ a @' C: }将 otherObject 转化为相应的类型变量:ClassName other = (ClassName) otherObject" V* h4 |% p2 @7 q' k
进行域的比较,如果是基本类型使用 ==, 如果是引用类型可以使用 Objects.equals(), 如果是 数组类型的域, 可以使用 Arrays.equals()进行比较。2 J7 H" a% S$ `/ g, }
hashCode 方法 5 i1 F, H1 h. w- t + O5 X, I8 y: ^5 B2 J1 I散列码(hash code)是由对象导出的一个整数值,散列码是没有规律的,不同的对象其散列码一般不同, * j8 s7 M$ C$ t! m l8 G. n 2 n# W I: b8 W+ b5 P$ x# UThe general contract of hashCode is:& d' d) B3 }, d3 l$ c& S) ?
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.& |5 e5 f& x' ]( e- }4 j5 G0 U
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./ L1 O9 C) x3 N+ M
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. 6 h/ C3 y. K+ v, S) L# E5 _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] " ]( ^0 g: ^# K. u翻译: 9 T- F- P" u: N. ?在一个 Java 程序的执行中,无论何时,hashcode 返回的值都应该是同一个5 L. U7 I/ l9 t. [
如果 a.equals(b) 为 true, 那么一定有 a.hashcode() == b.hashcode() 2 p: _7 \) G! k: s$ c0 ^- \% k4 k! P如果 a.equals(b) 为 false,那么 a.hashcode() 与b.hashcode()不一定不同 2 v. V r% S$ s0 x1 z. c8 d9 I8 t/ I6 gpublic native int hashCode(); 4 D( [( U! r, v4 Z4 X: u1; b. z+ m7 f1 b% X& o" E
以上为 Object 中的 hashcode方法,我们可以看到这是用 native 修饰的方法,表明该方法是使用了 JNI(Java Native Interface),使用了 C 或 C++ 进行实现。在实际情况下,大部分不同对象会 生成不同的 hashcode(通过将内存地址转换为整数)/ }* p6 n2 Y& @0 K% v# l4 A% K3 F
9 @5 F* |4 J9 b! q, P, Y
The main objectives of native keyword are:[5]+ j/ S/ P6 b' Q) G+ i2 b5 a
& l0 C4 W6 g0 q0 Y, S G
To improve performance of the system.$ W! P9 }7 c; j, |" |/ v
To achieve machine level/memory level communication./ e/ k2 a5 A7 K( Z" g
To use already existing legacy non-java code.+ { Y: E4 M [8 l! }
测试代码: 3 S- E. \1 G; D% }' ^: q/ S6 {1 P! A* ~0 X8 a
ublic class HashCode {5 i$ }# \6 R) ?# _+ M+ o. t6 O, N
) ?. l1 l( v2 K8 f% v( |. h
public static void main(String[] args) { % z. B: l7 G3 j, y0 Z' {6 A' W Map<Student, String> map = new HashMap<>(); 6 M& u9 l ^9 ^4 w Student s = new Student("Alice", 11); : O9 K# W0 ^' B; R 1 ?, i' I/ ~( ~. t0 V map.put(s, "Alice");/ |% L7 ?3 h v; T* k4 |
map.put(new Student("Bob", 12), "Bob");% o7 h7 I, ~* b: O
map.put(new Student("Cici", 15), "Cici");. `$ b, F( c2 n2 ^$ V
" J0 o3 c$ z; @/ w) L" R
if (map.containsKey(s)) {' q' p1 t- c+ J7 w* u
System.out.println("bingo"); // bingo2 B8 T, T) h: J6 z, \
} 8 \( a3 E9 M' Z8 z ( F- U7 O& l6 L) d if (map.containsKey(new Student("Bob", 12))) {2 g( z V4 k1 d3 e
System.out.println("hello"); // hello 5 u6 T1 N. ^- _ } ! x+ S$ g# X: p8 U# m5 | }2 d& b) w. v! G2 o! s" O
- V/ U7 w/ M z; e. H3 ~4 d7 F$ Q}' m9 G, d$ f& T4 G2 c
; d! I& }( A4 k1 q6 Oclass Student { " O" T. {' R2 T p private String name;3 M* q6 G' y" ], G
private int age; 5 j6 f. B- w, {9 R) z" h5 `$ @( ?- [& S$ U9 u& N; j& `7 r
public Student(String name, int age) { * Y4 v b8 r' r% z" E this.name = name; 3 h: z+ b0 Y4 _8 L9 w1 ` this.age = age;) z' a& R2 p9 C a; j& }
}1 u/ L( h; A, Y. ]. K* ~7 C k
/ v6 v& G& T5 v' s8 ~' J @Override 0 o4 b; u, V2 ^" R) R" B public boolean equals(Object o) {; Y: }! n- P6 K5 ~1 K; ^8 ]- ^
if (this == o) return true;& i% T. ]& `* _8 _
if (o == null || getClass() != o.getClass()) return false; 8 h6 N% r# S$ U, ]0 Y3 q7 W4 j Student student = (Student) o; * z8 B& t% y2 I. O i) Y1 ` return age == student.age && # c" y- W4 k. ~" B& B& i7 G Objects.equals(name, student.name); ' t+ Y. n4 C; k- p }% v; @$ g3 Y s s
" z' ^% P! J5 D& h- @" G8 H) ~
@Override % A$ W: q! A: F K public int hashCode() { # x' y$ \, p) c# s; i" U' l return Objects.hash(name, age); 7 w5 F! Y7 r8 z+ \( E; F% K+ l }" c! u3 P& [' s, M5 i+ X8 P; v
} * T# U% d' g; |/ H; c1 B5 \7 Y1 8 O1 K" @% q) v1 j* L2, K5 t- W( A5 r; l
3: j3 y; g" o- v/ C% m1 H
4 ) d% h' }% T, I {/ A; N+ C5$ n; ]: p( Z# F2 F
6/ Y9 H5 G- }4 P5 _6 [
7 7 S; Y: x7 m( t; n6 `; i8 * L9 S) O! d8 W( k9 2 y5 o) G8 a3 P) j10* w/ P }+ {! C, @" h0 |/ L5 z
11+ `7 c1 j6 a# b% j5 ]5 q
12$ Z. y. q0 r: u4 s, x0 v+ _
13 3 d, W0 |' H( R& b, _- d$ _7 u144 S5 U, o' o- V4 x
15" \* \( w5 A( J9 ]3 U9 Y( H* e
16 6 e" O' O; N a! ]3 S+ P$ J17; ^1 x7 ]/ w, h* z
18) H+ o3 u8 n5 _4 V$ Q# D( O
19; E0 D# w) b# N2 v7 D& }; _/ j
20 - s! y. `! m; x5 \9 g+ Q214 l! A; D, o8 x' T; P
22 4 y$ ~+ {- `6 `# ?; K23 ! S$ _% w. w8 r& M% h24 a6 }6 ~# z+ V6 p25 3 ^: C3 B% I# t7 |6 b26 : o2 |( X9 o ]27 0 n3 X; r6 r9 J- u3 C28& W3 O& r- o* e. t2 K: _
29( d) ^0 L$ T6 Z" v
30 , J" t( ^2 S- L: k' `# w8 e31! w4 l4 d; `7 X' m h' x
32 * B6 H3 Y! ~: Z' @$ \/ k33 9 g6 o: K1 m+ }; I' `/ s3 R9 R! g- T }34. k/ K! M9 f7 j" e8 ~% w
350 O! Y' v5 G/ e/ R; l% l: {& _
365 b" k$ A1 a1 Z1 e. Y2 M* f
37, e3 E- K7 ?! p/ H
38! u. M' e4 P' V; [0 F! m& h
39 * L) Y, X/ q& B/ O40 8 B) Q6 L5 j% f! i2 C1 J41# b$ B$ a/ W! T0 D% b; C/ Z
42 ' |+ L* Z; j' W" z- K432 W/ J+ ~ v4 a% q# G/ p8 t" F/ K7 L! q
44 Z8 c7 D9 d S/ K5 l% C上述代码测试了,必须重写 equals 和 hashcode 方法,才能正常作为 map 的 key,如果有其中一个方法没有重写,都会造成 “hello” 不打印。3 A1 N4 {/ ?, B& }% R+ N% F
& a0 I6 ?+ H- p( E* T" h8 c
clone 方法 3 B. C7 [8 u: }- u( S8 W& C: q; D: {, M% r
protected native Object clone() throws CloneNotSupportedException;$ w7 t$ H+ F; z
1 ! M$ |& C3 m* ]* @观察源码可知,clone 方法是 protected,子类只能调用受保护的 clone 方法来克隆它自己的对象,即只有在 Person 类中才可以克隆 Person 对象$ j; j' i0 d3 O* e! r3 u, m
( s1 m" U9 b. t) n8 {% spublic class CloneTest {& [6 W8 T; A) V! k9 ?" u g3 F
public static void main(String[] args) { ( S) p" x1 b8 @- Y/ J0 b Person p = new Person();% @6 r9 a, ]! a2 A
Person p2 = p.clone(); // Error clone() has protected access in Object. p& U& N2 q6 H6 g& i4 _. \1 ^
} " L6 ^* y' O) U. L} 5 L# A/ k2 p6 k+ L$ I1 P1 [ , @& {8 n8 H5 r2 _# K; ^class Person { . i. B4 [1 _& c1 h9 } private String name;6 r( G1 n1 D; D6 [% A
private int age;- K8 j9 R# O: d$ P
}3 Q# W q4 `7 ]3 B8 M- Z+ p
1 6 C! {9 C" `: d$ l7 R, ^: z9 |2 . \2 a" O7 ]6 o5 ?3 M3 X o2 y3 ) D. e w( l* L/ D+ e4# R7 n' }$ R: Y0 a
57 Q: y0 z( i1 s# \
6 ~) R: X; u* {! _' V; _7 2 t4 N/ ]* c3 Z0 ?# |( q8 - v2 X! w1 p$ L) [' p9$ b/ `: u6 w+ D, g, H( @0 g* m
10 $ P! R4 {$ g( u9 j11, {' t) S$ O1 m" p8 Y7 {, L e
我们可以重写 Person 中的 clone()方法为 public 即可调用$ d. U) P' P* K, L) {5 u/ \
: E$ A# v! E i6 Q6 E1 x. N
public class CloneTest { 2 I3 N& \2 c; T7 m; i/ b public static void main(String[] args) {7 n8 A/ |) \& R; _
Person p = new Person();/ I* d4 H. j+ ?2 G( N$ u& o
try { . I9 R( `$ u. d( P Person p2 = p.clone(); ( F& p. o9 n6 j5 h } catch (CloneNotSupportedException e) {$ f1 J! Y+ [: l& q5 P2 K: Z; i
e.printStackTrace(); ) x5 T" \$ G5 U9 M } 0 e: v: a0 I2 b8 t } W6 t9 ^% C0 j! d+ Y
} , }2 ]$ a2 A! \# g, s( B * [( l/ o$ [! W/ w$ J+ g+ I( Eclass Person { $ z$ c$ l" ?+ Z private String name;9 c* q# A# d3 R9 G: S9 G% g
private int age;4 d" H0 x4 Z7 l2 @: Y. g0 |3 M5 g
: F+ H2 z; S( q$ C @Override # r0 b2 H4 b5 u) b protected Person clone() throws CloneNotSupportedException { ' P. \/ V3 r4 j0 E4 i/ f. Z0 c return (Person) super.clone(); 4 n# [ [4 N% x/ g* d6 u' q! ? }7 T8 \( Q W# l( f* D; n5 A
}5 Z9 s7 B/ b! z6 [. U) }" l
11 s* Z v$ v: j1 ~
25 y$ C; z2 ?1 a8 B+ Z4 T! W
34 w6 @6 \! f9 f7 Z
45 b% H. Y, w% X
5 f& O, U2 G' w0 z: H63 D1 h( s5 q. L+ r. P. q
7 # e* K) q: t L9 ~" t; X8 " n8 q4 d J- o) K }1 i" i5 }5 l9 C$ a5 q7 u+ j, K4 W+ T7 e7 q10 / O3 X1 h. m9 u9 i6 u4 o11 7 L* I; I% v) R0 X2 g* h125 k9 t: h- m3 B( h, [+ a
13 ! }0 ~! K" A. b, L6 o, d14 # _# R7 l; k8 D15' J! x! W e# t& y2 c; W; O
16( V8 @( [8 U$ h+ Z; V; @6 p! C" g
17 9 d" H( O6 Z) I$ m18: Y+ o& |: s Y. D, ]
19 3 {- O8 k& ^/ Q9 ^2 f* O5 N/ m20& ?6 u* q3 B C( g
但是当我们执行代码时,还是会抛出运行时异常:java.lang.CloneNotSupportedException,这是因为我们没有实现 Cloneable 接口,这个接口是一个标记接口(marker interface),只是作者了解克隆过程。: c6 A3 d3 R% J$ ?) @" Q
5 Z. d& s. g: e! h$ y$ I8 e浅拷贝, I+ ~% a; L" d) ^+ g* @; o- }
, c0 @- G# M, p. l8 F N
使用默认的方法得到的拷贝为浅拷贝,即拷贝的对象与原对象中的引用变量指向的是同一个1 _& I& q" b2 P5 I3 E
5 D2 j P# m+ g- jpublic class CloneTest {4 v/ ?+ u! U! u* Q2 c k% D
public static void main(String[] args) { . g+ x' G9 W1 M4 Q/ l5 r2 o Person p = new Person(); D& z+ _: X: d* |3 S try { - {. h9 J' M; \ Person p2 = p.clone();+ @- M6 r2 o! K8 j: E9 F
System.out.println(p.get(0)); % e6 G' ^2 u3 l System.out.println(p2.get(0));. L5 b) w) n4 A
p2.set(0, 8);6 P9 B* G" @! A; r1 I! Q2 Q3 V( P9 ]
System.out.println(p.get(0));/ t5 c, c2 ~. e) z3 C9 ^1 |
System.out.println(p2.get(0)); , {% j N, K1 _7 O4 \ } catch (CloneNotSupportedException e) {3 e& P; y9 t8 r# e* X, W' _. F
e.printStackTrace();* v8 v# f7 s1 S# K. T J4 @
} r0 m( E" |1 b3 b, _
} & P6 D) G: a" W* }} * b5 R4 s! e$ A( C 0 h4 U: x$ a8 vclass Person implements Cloneable{! ]8 F6 ~$ s, |3 V% ^6 q
private String name; ! `+ L1 s% ^ Q, v3 ]+ } private int[] nums; 7 @/ r" B* s% A! h" ? Y7 o3 w9 B: r% ^% B public Person() {4 c$ R4 g$ Q3 P4 Y4 ?2 @) T
this.nums = new int[]{1, 2, 3}; & C5 X/ ^* H7 ~" X( A; ^3 T( n6 T } . |) l) w0 U- @ |. } / | |. K6 u6 W/ k) v& q public void set(int index, int value) { " O' V: G J; B% }. H this.nums[index] = value;& M$ Q4 D2 o9 b. K
} 0 l r D* U0 \ ! g* C; M1 M1 G/ E3 y public int get(int index) {+ c* d) M8 \' `! b6 A' v5 D
return this.nums[index]; " x+ y8 P, {, M }* m4 D3 I6 M4 M/ Y
. Z+ S& f9 l" l: d: f4 y
@Override) S/ }( `4 t; i" `1 t- x" l% l
protected Person clone() throws CloneNotSupportedException { A& W( b0 {! F: L% y& e& s3 S' | return (Person) super.clone(); 0 |1 l6 l3 }9 ]$ N9 C$ a } 2 m. Y2 E, x) h3 E0 w% ~/ b4 N h} 6 T8 ?7 ?5 p: S& Y. d% B- X( Z) l# i% ?2 R# d8 b
// output : e: p+ G' l: n& x// 18 n# p" \2 q" Y: b) J# O9 R7 i
// 1* a0 A3 |' u5 H0 ?+ s8 d$ P9 \* d
// 8 $ X# c6 m8 |( i0 p7 \; E// 8 2 W1 C4 V- S* e14 B& p1 d* E% h* w5 [: [
2: k: Z4 A8 n; m+ h
3 , X6 T4 H, S9 z9 O4: M4 b+ T$ c5 S$ }; h k/ a; l7 q3 d
5 J. }8 V* g# v8 V! u; J6 . b; c. d6 y. J7 0 f0 B5 _5 d" a( X$ S- f8 . d# o) r- Y% f: z% \& ~% R' ]9 0 Z6 h6 l9 n+ y5 k109 l; J1 \! R" e6 w% Q! L! l
11 6 M4 L- Y& O* v6 }: K! w) a12 . [2 c& w& g9 H/ q13" l# _2 k" X5 _% I1 a* k: T0 y9 Q7 G5 G
14) g) r/ A: ?" A0 ?) B" G/ Z7 d
15 2 F2 {% x+ G" t6 t2 u16& N% J" T4 j- u1 h$ W% ?3 y$ W
17. E$ `: F* n) l
18; \2 T. h6 j) S/ {9 `4 X1 w+ W9 K n% |
19, B* e5 v) w) G( ^0 J4 V, l: R
20 ' C2 P. i b$ U3 G, |* `, S: L21 ' ^9 e: B3 j5 d# k; @7 e' k22 8 `1 v7 O8 y/ x! Z4 L: |( X23 ( r. K" h( R+ t. v7 C( f24; q6 K2 x# a! e
25& p' f3 g8 F! k- q
26# C# _4 U% ?7 l! _. R7 p3 E: a$ {
27 ( q3 c \( _1 q% X28 7 R( P# N# g, ?9 g" L29" j- x$ m* F% H! L
302 v2 J+ O }! W" k" K B9 l1 n3 n" b
31/ v2 G; x1 V) ^8 z g
32- C% ^( H: P" A1 h! X; h
33$ k1 |4 v( j5 f5 x; }9 g3 ]) N. I
343 L! g# g" H$ }: _
356 S! w- M. W7 Q% f L3 ?8 C) i
36 2 H( w" [0 [: M- a& D37- `, q8 J6 H9 }1 i
38, i/ K1 x; K( Q8 }
39 8 X7 v I" |6 I" s( k( Y40 $ S! `& K. X4 Z$ O7 M- R41# ]5 n; `6 n: j1 J) Y2 G4 q. \
420 y: ]' j: g e3 P" t/ X: _
43 . w) p: V/ F, o# H5 ^- ?: E深拷贝9 P( x/ R: B& g9 O
& B7 ^5 d& b ?4 r9 t为来解决上述问题,我们可以使用深拷贝 ~! J/ R! K! n9 k o- Y, ?1 J3 v% h8 J9 @) J8 c
public class CloneTest { % K9 U/ T" K) X* \ public static void main(String[] args) { }0 e5 [+ Q7 b) m0 O: d2 R
Person p = new Person();' V6 D9 K' ?4 F; {
try { ! n. X. y+ | S& w& H Person p2 = p.clone();, ]8 F, j6 a- \5 f6 c
System.out.println(p.get(0)); $ E- c: l3 H$ o3 v$ [ System.out.println(p2.get(0));; x% m# v$ j) o3 ^. D
p2.set(0, 8);# I. I+ h0 P+ N
System.out.println(p.get(0)); a0 ]6 N: \ S$ E' i% A- ~4 d; T
System.out.println(p2.get(0)); $ I0 {. Q$ i' V' Q" H } catch (CloneNotSupportedException e) {- h8 K* n/ ^/ d- K6 \# R" o; B
e.printStackTrace(); " @) R/ _7 p! r7 t- {, [ } + ~ R: i! ~" X: s }* _7 ^/ Q% s/ i, R
} q; t- k9 q; y% H2 Q6 N5 K* U 5 \6 K8 X* f: qclass Person implements Cloneable{ & t/ S" e+ Q g; \- Q% j: z' ] private String name;$ I# M+ G, a! v. K# l& W* s: E
private int[] nums; ; D0 B6 Y* C6 i P. }: D0 H) b4 v' r
public Person() { 5 C! H: L: Z1 D! |5 T this.nums = new int[]{1, 2, 3};4 b( w" F- D% c
} 5 F' J. B+ z7 B7 z7 R8 O: M1 X% P$ j" I4 J9 H5 x$ ]! G
public void set(int index, int value) {# E1 i+ g' v2 d9 ~! n) M
this.nums[index] = value;' G) Y! C# M! }1 `
} $ {7 r; |+ q: r s) P# l S . w3 Q4 s$ k2 c public int get(int index) { 3 u8 q* L% q4 s" Z return this.nums[index];8 Y0 j; h e+ y7 F
} 1 }/ ~$ R, v! V: d! ? : c7 c: g6 d' D$ P @Override$ z" v! p+ N) q$ a& R M
protected Person clone() throws CloneNotSupportedException {, E$ N$ W& m3 l) N
Person p = (Person) super.clone(); ! Q0 b& C: j5 ~8 {- U6 Y; s* G5 ] p.nums = p.nums.clone(); // array clone - S: {! v5 t* ?- G' V3 Q* t: q2 T return p; ) F7 `# O$ D% \% N0 }. @3 h }4 w1 M0 O, a2 R& S ?
}# X% O- i: p# i9 c, |
- {2 J9 V2 m0 N' Y, `
// output / v. s/ {* A" ~' B4 c7 N// 1 ! ] }* s$ j0 B7 u0 L7 T2 N// 1' n- z3 B# E+ s; Q0 J8 f
// 1% I/ }. {! Y3 ^* Z' U' }, a
// 8, @# m0 G. k Y. w1 d& x# \$ X
1- w1 X: [+ X, @# P; P4 L
2: E1 o' d2 w( |5 a3 \6 r1 U
3 , K/ _% L% D8 _/ b41 E8 C( m2 v6 ^ }. G. g
5$ b; y5 `2 ~( E0 O6 Z# N
6 - A/ a0 V; ~2 I0 W# b. u) C7 # G0 E! r6 G9 P6 f8 g8 u89 d+ J( O( f# M# F- z# A; b
98 c7 [* U; L. W+ E$ M
10. Z4 u) J" d) J, [
117 G5 ~; U7 P1 F) m* V% w5 v3 D( V5 J
12 [( x% k; @. A$ E0 u1 e13: m7 J+ U0 q% G$ F! G1 _+ k3 o
143 f% {4 x6 C! x# ]+ v) g" V
15$ @$ v% }) o* D' N+ \8 j4 L% K$ r$ g8 E
16 1 O: c" k/ S9 @% }# U/ b17& [+ _6 q: r' Y% g7 O$ \
187 D% b% d6 L. f& Q% l2 H
19- I; k+ G5 B7 h& ?- L. U
204 d: N+ l2 f5 `2 }& L) t
21% }* d. K3 U! D
22* x+ X0 [, M) q9 @1 A
23' z! a% W4 y& [) X' z
24 2 | d* K. o" D25- {; s- O$ K. ^4 i7 v
26 : o/ x* [/ y! f2 ~! q$ x4 x27 ; X0 A/ W( r; {; {28 9 U0 c8 ~7 Y8 ?5 z; |+ I29 3 F" i% R! P+ e1 R* t! U' _- p30" o2 C1 |: N+ p6 q
31 2 a. F3 ~8 ~( G0 B% }32 . |" j- Y7 S5 p9 H33 # T* n+ }6 p( M& T# U# ]344 R% G E2 N, F* D! f* x0 Y
352 o- |* t: n0 _+ }/ q
36 3 x, Y5 J# u! `- ~! h9 F373 U9 ^' j5 U& b4 o) j; W, u
386 ^1 P" _2 |3 ^& K' e9 S( `, A( q
392 y( I3 G( V6 B2 S' `0 T" M! a4 d
40 . u5 `8 f7 m- F2 K% V0 {0 H$ b41: S. G- a$ H5 `/ |
423 d/ @& m& i3 B% J i, q
43 7 c; u1 [: D3 s! [44 7 |- z. w# \+ Y+ q+ a% ~/ W& f0 L452 I$ s4 B& o8 x7 @1 T
注意,数组的拷贝可以使用数组自带的 clone() 方法,该方法为 public,所以可以直接使用。若有其他引用类型变量需要拷贝,可以使用该类型的 clone 方法。3 ]+ S. S; C9 W5 Z! v9 j+ m
1 L6 ?$ ]; C2 a/ m, g, Z* W9 ]4 v9 Pclone() 替代方案7 y! T0 c: @ U, i% g& o9 G; i
6 S: v* d6 B8 N8 H9 I
clone 使用频率一般较低,我们可以使用拷贝构造函数或者拷贝工程来拷贝一个对象: 7 V4 x/ [& f; h7 Q$ m1 h) m, J2 b* Q3 x' d7 J' G- s4 B" ]1 l! G
public class CloneTest {/ b/ x+ I: W& `* l& A
public static void main(String[] args) {) \! g# |$ c& F. ?6 P
Apple apple = new Apple(); $ k/ P9 T7 J% b6 U9 A% c& R Apple apple2 = new Apple(apple); P. X# x' b6 i+ U) o. s
System.out.println(apple.get(0)); . K/ U, z2 ?2 V9 Z7 F System.out.println(apple2.get(0)); 1 p7 [9 I9 f6 l0 m( ]2 Z0 m* i- b apple2.set(0, 8); 6 S( w- @2 f/ p System.out.println(apple.get(0));: O' B2 t+ T# g2 W
System.out.println(apple2.get(0)); : V( S1 H e- } v% e3 k8 y$ N } - b9 L7 @0 n! ]4 G1 Y# Z}9 @5 ]! @/ @6 y% X! f3 G: ~
class Apple { # ?" w8 v7 j8 s- T private int[] nums; / t- g5 ^' p: z3 d private String name; + J- j, O& v2 g* p4 _9 q4 h$ y8 y3 e% S# ]0 f
public Apple() { 5 {- D. x1 O3 X6 w7 h9 F1 w: s8 C this.nums = new int[]{1, 2, 3};& i# q0 ^& q3 M/ ^
}2 H! M4 k5 ?3 V
1 D! h. b4 S- _3 Q5 j public Apple(Apple apple) {: R6 @+ Y) ~# X6 z/ x
nums = apple.nums.clone(); 6 y& Y4 N9 o+ T' D8 ?( X( ^" h/ z name = apple.name; ) H! i) @9 v1 ?, R( [& g* y% D- a } : C9 _6 r" G( q/ K! x1 b5 R+ P5 i) f7 J2 R( }% _4 i+ |5 R3 t
public void set(int index, int value) {+ |( @2 g# L& R1 k8 X( W1 G
this.nums[index] = value; ' {# M, {' e8 }1 q0 e5 ~ } , s/ e" l9 V; q, E& s. S2 C 7 t9 x9 G( z0 O public int get(int index) {; j0 h" n* w2 N+ @4 [
return this.nums[index];$ [8 \1 l5 h, q" y+ c m
} 9 {- w4 V [& p. E8 I0 @}8 B3 k9 @" H7 G. i6 [
1 `* u/ J8 x! s5 t* m1 T$ C: N. [// output7 S) n9 e5 O: Z# _9 ] c2 l
// 1) j, }. V1 c7 W7 x- s
// 13 W$ l4 Y) L% Z
// 1 ' m6 N! E" z. `% {// 8 : s% K$ Y% p. B* T9 ]17 c' X: r, {" l/ O( f) p- e
2/ _0 O0 J; v: b7 l! a' _
3) D- x4 `+ s8 R7 U8 }7 L
4& v# W4 c; B- r! `4 y1 [; J
5 % k5 R9 T; G) R" C( `. N61 |; m( e7 K& q" x! X* }7 S& h
7* ]- P# u/ o9 \4 E
8 m4 l: f8 y& B* ^0 x$ p6 v! ^/ `
9 3 |- V- E2 O+ v. x; \* M106 q4 T# h# ]" ~3 L2 q, p" l% I
112 J9 X p W" W Q0 _* S
12 9 I' [" C* F- s. A) H13 ; ?# {6 l H+ `* @5 \14 {- U& M$ z4 K6 Q ?
15$ O; v; n0 z# ?, Z# U( G" `# v6 K
16: B- \* F( }# |: w
17 : X- v" c: Q( x Q* `181 p; m# a/ G( r6 y& P1 Y5 f
19& d/ K: ?, {1 O
20& Z7 r3 O0 ^+ o4 G/ N6 N
21 4 {8 `8 m+ G4 D0 n2 m22 # x* T5 X7 z! R7 P$ ?3 I23" b+ r: H0 B- _# f; G* ]* H
24 ' b1 U% f# m/ w. L25& c& K, M7 V; x9 `- Z3 S
26, U2 I1 @8 V; Z4 l. N3 L1 B3 D1 U
27 ! A9 g" R4 q( s28- c1 x0 a* S n1 y9 ?# Z. \
29' B; a- d, a+ K. e' h4 d
30) g. w3 d6 q+ N& u4 A- I( s* n& |; p
31 + O/ W) i8 W4 I' u0 m32 : n, a0 j' [- r( H* M7 S1 ~33/ u7 r, E5 v8 h
34 - C* s* h9 c& H% l+ f' z350 I4 V/ x1 s) }" v ~8 k& o$ d( ?
36 : N) r7 |; |/ {6 ^5 C% O; D37$ D) k3 {6 R0 t" d/ H9 K5 R& I
38 1 T% h2 z. F' T7 R, }1 tReferencee& m6 p, y" c: v! Z" ^ N. e" ^
% B- w) B8 A8 Q, e' i _Java核心技术卷一' Q9 y7 D* C2 s: _) S# Q