0 g9 b8 o# N) o4 s8 `instanceof ) K7 ^9 f+ j6 R& p# h# C0 f$ B( @- D# U7 |* g/ e
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]. X& ^9 ^4 W6 A: J1 x( k. _
简单来说,instanceof 是用来将对象与类型进行比较,我们可以通过它确定这个对象是否是一个类,一个子类或者一个接口实现类的实例。" i% P. ?- K( R6 I6 e. X# U7 W L+ l
' }9 q+ a- y; U# W/ w: o0 p: F' [public class InstanceofTest { $ k! O" f- n' ?8 M6 f/ v public static void main(String[] args) { ! h, a5 l7 n J1 k% f V7 {9 n Father father = new Father(); 5 D$ o% n9 s* T( T System.out.println(father instanceof Father); // true/ C6 ?/ h8 ^; s: r, [
System.out.println(father instanceof Son); // false0 p$ A2 }) }2 l/ s7 y4 L' v @/ ]
System.out.println(father instanceof MyInterface); // false+ ?" }* o4 |( b# {% }/ ?
System.out.println("------------------------");5 B' F6 C* ?4 t) K0 Q
# O% ?2 j' F% t9 M$ y
Son son = new Son();3 n3 W# F: |0 b% L( g
System.out.println(son instanceof Father); // true/ s3 S O) y A5 Q& m5 Y# Q
System.out.println(son instanceof Son); // true 0 v6 \0 O; ?+ W2 M System.out.println(son instanceof MyInterface); // true : a3 R0 \$ Q) J3 {; X6 Y) E& Z& Z System.out.println("------------------------");" J2 y0 X( f* l9 }* U9 P' N/ z
4 Y2 \1 \+ I5 q Father father2 = new Son(); e5 J/ M' e; ^ System.out.println(father2 instanceof Father); // true9 b0 _. [( g9 x( R( o* D, ]/ Z
System.out.println(father2 instanceof Son); // true% N: X; ? U4 U2 b- j! ~ I
System.out.println(father2 instanceof MyInterface); // true8 x' S3 X2 J2 h# o5 o. t: V/ b
System.out.println("------------------------");* j# C& f6 C, F6 J& D4 m' n' R1 y
}: n. d) p- n. f
}* z8 u9 }* @' o( k6 U; h) n
class Father {4 w7 w/ [0 t( Y
V9 P) O U3 k \8 v& c} 4 H7 @. G& K3 X& F0 x, o4 ] 9 f' {1 ^% ^. A6 P# U. K; sclass Son extends Father implements MyInterface{ 7 J/ `; |8 h5 H$ { & X# v# n2 ]0 f0 W: ^0 j}5 z- g- v4 G) G3 K3 x1 t+ z
2 D$ D- ]0 d2 L- P tinterface MyInterface {4 z5 }, t$ R) ?3 J/ G
. X; R- i: z6 t} _! q! X1 z! C" R% w
1 0 S" J- y) e. Y* v2; L( F/ ?/ U$ _$ U/ m# U% y* e
3 0 Z* m1 V. c% d3 K5 ~; i4 ) E( M- Z- ^9 b7 y0 t+ i50 X/ {$ \$ v4 ?# s, x* K7 q
6 3 {3 z" K8 ^9 o* O. [- X9 @9 }72 }; `+ E4 R$ m, y7 C0 L
8$ z7 _/ }5 R( T8 p+ y
9# y% k& M u' v6 w. H$ X
103 u1 S' j% g% c6 v
11 " h9 ^8 R7 G7 B5 S/ s, J12 / v( M5 r; a4 z. i( `9 j131 y. \9 z! w1 T% H
14: C# d, _2 P8 b7 e
15 * g5 N( N1 x& O, `# R& m4 X16 N0 S+ S0 T5 F: p6 o! ^! @. \17+ w+ _9 g7 y; E0 y; w, N: x. h
18 $ c! {- I7 ?/ r+ V8 [) m19# \4 [) E% P/ E: F1 V3 a% A
20; Y @8 E# Y& H- n" ]; c# O: t) E
213 J5 X0 X* |; P% x6 L3 A
222 |4 Q: T1 M7 i1 x/ L# R
23# \( n4 F' E: j
24& ~# {: ^+ `3 Q* P0 H" M" r6 z3 {6 ~
25 * E' `* @8 E/ `* G, }/ U) } f' B26* m% k* I8 u) f7 x8 \
27 4 r( ~* h4 W, |7 l28 4 E5 `9 U+ _' O9 A9 e! Q3 S; @29 4 c. p( l7 `3 q' q, u2 ]30 4 ?) x% s- O8 b# o- X: m9 A31 9 @. y# M+ S0 f! o% ]# j$ {32 1 x9 p2 w" r' E. E) W4 u5 z: C% X上述结果我们可以看出当父类变量被赋予子类对象引用时,其结果和子类变量是一样的。 $ S/ m5 K6 z4 M5 T+ ] X' ~" T4 x0 E* I. a" f5 [getClass% B6 a( S2 |, s0 @0 O" y ^! y
6 @2 E+ E" k8 R7 v4 Q
public final Class<?> getClass()8 W) y) ~8 J( i# b. e6 _1 Z. |
1 . \! l$ Y _# j' |. @: Z* X$ x, ^4 fReturns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class. + Q( a7 {: P% ^8 Q1 Tpublic class InstanceofTest {; ^$ N) d" D7 N5 S$ l
public static void main(String[] args) { ; I4 ]6 H: s8 t Father father = new Father();3 H4 V4 `: ]2 A' n) X5 w9 ^8 c
System.out.println(father.getClass()); // class Father$ j: V; t% c8 N5 K2 }8 ?
Son son = new Son(); 7 T% A5 }# }6 {! H7 | father = new Son();, { W; ]- s0 C; H- A; U7 _/ U
System.out.println(son.getClass()); // class Son3 f7 n& u( P* L
System.out.println(father.getClass()); // class Son- M5 y6 r7 m! l, e* j+ k
% i' J7 G7 i2 y4 Z/ }5 }2 u; G } * v. h/ t4 {' i- n% U}9 n7 Z/ Q K9 c N/ {! e
: R) E9 I6 B: G: Jclass Father {! ^& g: m& X3 Y! U
! m4 \+ f& ? Y3 @' C5 A+ P# l} " Q) C3 ?; b) y 3 ?; H1 x4 [; cclass Son extends Father implements MyInterface{ + L) u( t- C* X8 X$ _$ g% x$ V6 k* E& l! L* K
}7 A3 r0 T; }0 t9 T
$ [$ l: G" U% c7 _" `
interface MyInterface { 4 g6 G7 k7 Z, X# l1 f v5 d7 h / `& v F/ f+ W+ V0 G' @! }} ) Z2 h# z0 [1 n" F+ l, d8 `1) L W+ G+ Z. y8 u
2 8 R* U" F) Y* X9 D# n) Z' z6 s; n6 V! W3 % e* d q: a2 ~5 ?, L9 S! J4 & `, D: e# H6 s; V3 X5 ; i3 D6 }, C' M. ~" s. z6/ q6 e2 n, Z4 m; ]; g6 V
74 m0 G! r" |. D/ v
8 2 k' v/ c, @) ? w {9 : U, l% t9 T/ U: ~5 V; K3 P; j- ?10 ' e* W6 N+ L# X* E% ?+ |: i113 r" K/ }6 {+ u: B7 w1 j; r
12 7 j* U: k4 h# e2 {2 A" W13 ! `2 ^- V1 l/ Y( {! H1 _ m14 0 K1 n8 _2 i8 ]" H7 ^. ]15 ; D' B9 z8 {8 i, R16" Z t" m4 b! H) c2 q
17" r: G& N! u3 @" w1 i# C
18 . J5 T$ ?0 K( a/ [19. o& o' V; Z% F& f/ e8 h k
201 l, a! L2 i6 U
21 # i9 N, y5 |6 z2 x: R) G, Y8 T22. w* v8 B; K( T0 j
236 g" W' B* u( y0 |
观察上述代码我们可以发现,getClass所返回的值与对象变量此刻的对象引用有关。. t& E `2 Q+ z& _" ]! t
, n/ m! Q5 e$ S! E) K1 B% H相等测试与继承( M" ]; P% F. R; m [7 M* B; j4 v
: ^) i/ b5 L1 D% C
我们来看,如果复习的 equals 方法中使用 instanceof 来进行相等判断,即使用以下代码片段 ' Z- |3 E0 C2 |. A6 L! D, X: I: U( H+ F
class Father {7 P% ^4 s8 I/ G, O1 I% a3 P3 l* u
private String name; 9 k O6 T# ~0 x3 f+ u# E. H9 `/ F2 K* I" [
@Override' ]* q" c5 a$ N$ X3 i( n
public boolean equals(Object o) { , a6 I0 b) A$ O I/ h if (this == o) return true;2 K- O* P8 @" p7 L
if (o == null || o instanceof Father) return false; 3 G/ z; v3 N0 ~6 g% p$ Q Father father = (Father) o;8 u. y* U! M9 i( s
return Objects.equals(name, father.name);0 j1 o9 z& A6 R3 \. B P9 O
}1 z9 F! o5 Z; G, f+ h+ L* \6 R
}6 f; _7 R+ B+ P/ }; [2 m0 i
7 j- g. z3 v1 M' F: c% y
class Son extends Father implements MyInterface{/ h* b2 H; n3 Q
private int age;+ _, H$ \- |$ ^( {
# p' B. s# h/ T @Override % v- G& k$ q C( _: W0 i2 K Q public boolean equals(Object o) {9 t+ m9 H X; A# L% t3 [
if (this == o) return true; 5 V2 g$ y% |4 V9 C) Y+ d: P4 c if (o == null || o instanceof Son) return false; // not good* h) G: c" V. @0 v5 p2 G
if (!super.equals(o)) return false; * k- o$ n" A9 c/ }3 n1 L) Y! w+ @/ R Son son = (Son) o;$ \+ m. O2 C t$ S) P' U" x
return age == son.age; ( F1 ]& j6 U" a1 ^0 A( { } ' ?/ D+ f W4 v: }}/ a4 M# h; S2 n1 d2 |1 S7 a
1 V, S$ x. x6 [6 b* q& }6 p0 f$ O
23 k' Y6 o. V A$ c5 f5 i# u5 Q
3 ! A2 [0 B! S& N! x4 $ x' f3 ?* c* S+ q4 H* d- s0 h5 , l8 v0 j, _9 L! x" i6 t9 J% ^5 J- p0 P7 Y$ J) x/ L7$ @. L7 @5 h& Y9 V
8 3 }/ M/ S) T; x( g9' n. {. e: m! |7 S0 t5 `
10 0 y; }! @) F0 g( M0 }3 X# Z119 Y6 c+ R5 b! c" ^+ e
12/ d4 d0 v% I+ Y* W8 L
137 w% q0 ~! S+ M$ [; l/ M
14 # R& i1 M9 k* B, U4 m/ n% N15 ( U1 z9 {( i' X: d' \% }3 i4 p16: Z, h/ ^) L' O* [9 W# U) q& A" l
17 * D! b* e9 Y* E' c18 / B4 o+ D2 v9 B$ K% q) _199 W% p2 i4 ~0 y
203 r {7 j" w9 r! \. U8 L8 R& d
21 : }! \2 `8 q% ]4 M3 W" v/ B0 H: E" f228 P/ y. S' H" q5 g
235 |& R o7 T5 L& y
24 0 Y5 d9 @- H" A6 d当运行 f.equals(s) 判断时(f 为 Father 对象, s 为 Son 对象,假设两个对象中 name 一致),根据代码会返回 true, 但是当运行s.equals(f) 会引用 instanceof 判断失败而返回 false, 这就不符合自反性。 & {1 V$ {) @# x( z, n+ F0 ^, S) D# A( a1 f4 r
综上,我们可以得出结论 . Z7 Y( X. C" F# v! C' d$ G ) p$ J7 N4 G! _) N8 I如果子类有自己相等的概念,如 Son 对象还需要比较彼此 age 是否相等,则对程序需求将强制采用 getClass 进行相等性检测。6 f; h; s* Y r/ X$ a
如果有超类绝对相等的概念,那么可以用 instanceof 进行检测,只需要超类提供 equals 方法,并且要标为 final, 子类全部继承超类的 equals 方法,这样不同子类之间也可以进行比较。 ( c- X' l6 U+ n2 |* {' B( ]3 A复写 equals 方法的步骤:# h% |4 ]$ q1 v9 Y( M. } `7 l
+ f4 a& K- |* Z0 g, y! I6 t, S
现实参数命名为 otherObject , E" }' N6 V: _. O/ c, z检测 this 和otherObject 是否引用同一个对象: if (this == otherObject) return true;5 @% w2 b' @% h% f j6 D3 s
检测 otherObject 是否为 null: if (otherObject == null) return false; & w' I( M3 D# r7 B4 V7 ~比较 otherObject 与 this 是否时同一类,如果 equals 语义在每个子类中有所改变,就使用 getClass 进行检测: if (getClass() != otherObject.getClass()) return false;如果所有的子类都有统一的语义,就使用 instanceof 检测: if (!(otherObject instanceof ClassName)) return false;9 r! T$ G* W+ Y) d3 ~, I K: K+ d
将 otherObject 转化为相应的类型变量:ClassName other = (ClassName) otherObject. s0 r2 Z! O# h0 M/ j
进行域的比较,如果是基本类型使用 ==, 如果是引用类型可以使用 Objects.equals(), 如果是 数组类型的域, 可以使用 Arrays.equals()进行比较。* I' ^: x& T4 ~" x' f9 h
hashCode 方法, D D, N1 m# [* H, v
% s3 ?+ f: d" H* |' X) v散列码(hash code)是由对象导出的一个整数值,散列码是没有规律的,不同的对象其散列码一般不同,' N! `8 J. X: G! c8 e0 H0 Y% V
9 [7 {. u- f- \0 `The general contract of hashCode is: # j: c% x7 c+ |: i }, G- xWhenever 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/ r+ O0 f) B$ ?+ ~
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 p: l& L( L* a6 v, O+ 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. 6 {4 P( C7 I" j/ |5 w- R0 |+ _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] , z; \$ x' b6 Z0 D" s翻译: 2 h* W3 k# e5 k( T$ _在一个 Java 程序的执行中,无论何时,hashcode 返回的值都应该是同一个 : W5 _" i9 L5 v1 L, w& X如果 a.equals(b) 为 true, 那么一定有 a.hashcode() == b.hashcode()3 `" d5 g) x# t6 H+ k; z
如果 a.equals(b) 为 false,那么 a.hashcode() 与b.hashcode()不一定不同# z& m; o9 i' a& Y9 `/ V3 k
public native int hashCode();! b3 t+ w! m/ _3 S
1 2 u( P9 Z+ C8 q7 w' `+ d/ K以上为 Object 中的 hashcode方法,我们可以看到这是用 native 修饰的方法,表明该方法是使用了 JNI(Java Native Interface),使用了 C 或 C++ 进行实现。在实际情况下,大部分不同对象会 生成不同的 hashcode(通过将内存地址转换为整数) 9 D: |2 b9 U4 j3 ^ 4 O6 _: ?6 q8 m# Y+ c, R: A; zThe main objectives of native keyword are:[5]& c6 w. j5 ]' Z* f( i5 w- d: A2 @
1 w% V/ R" Q" G$ d6 J! b( \To improve performance of the system.% l2 v/ l5 B0 f3 o7 ?5 `% i
To achieve machine level/memory level communication.$ ~% B: r, n/ [; P8 l* o2 t
To use already existing legacy non-java code. $ L3 G6 m% f4 c R6 R# r测试代码: 2 k$ h; ^! S, Y" z7 @4 r 8 Q9 Q- l7 y# a4 P4 [' V1 Public class HashCode {, Y6 b- i, n5 `; H+ u
- T/ j5 `3 I0 _$ x
public static void main(String[] args) {" ^. O9 F4 m' Y r# `5 x
Map<Student, String> map = new HashMap<>();% G% X, _3 J1 S: q0 p$ d! P/ C9 k
Student s = new Student("Alice", 11); ; g/ G8 E+ d! l. B( K7 q- O% H$ X+ Z" t" Z
map.put(s, "Alice");4 }3 G( i; A; j# h9 b* ~
map.put(new Student("Bob", 12), "Bob");# }& E& v3 k& H: X. u
map.put(new Student("Cici", 15), "Cici"); t/ Y, |9 X' }& m3 s8 M: N. Q) r( V6 s
3 z. i; H4 s3 o if (map.containsKey(s)) {) _, W Z/ B1 h; G7 G' W. F" m
System.out.println("bingo"); // bingo" L. t1 l: d0 Q! V' I
}" G3 p* \+ j1 L+ T1 l% g
5 r9 J- n$ L: |- x2 ]3 ?' J
if (map.containsKey(new Student("Bob", 12))) { $ i3 b" f/ b! I* K9 _3 U) A System.out.println("hello"); // hello, m" e1 u' f2 h% g* V
}4 Y8 Y( X/ L" I. _8 _; s0 `
}2 H% |7 H6 m0 X+ l
7 ]6 N" l% C; c+ g! s( t4 J+ H public Student(String name, int age) {; B7 W% ^6 Z9 ?. f* O, v
this.name = name;0 r2 n" I1 X( ]& w6 S; b
this.age = age; # W. q q( p9 N* C4 k }6 M1 c1 \$ g8 S! S4 Z. t
" A6 J0 Z- ]7 }7 K" `
@Override6 i' r4 E4 |+ x' d/ B$ S m* R
public boolean equals(Object o) { ' i4 Z, f/ d3 b- ]7 e& T2 G+ @ if (this == o) return true; & I2 V* G* L. X" k0 f, g4 x( _ if (o == null || getClass() != o.getClass()) return false;$ J) b Q X1 }5 `
Student student = (Student) o;% F$ b" S2 {7 F/ I- y! B& h! M
return age == student.age && B. h* r; H3 o
Objects.equals(name, student.name);' {! V3 V7 S- \! ^7 o1 M; O
} ! a6 F; P0 F3 w0 A0 y% ^$ V5 J& L7 r1 T! }1 v
@Override $ X1 U7 Y8 Q) r% {1 X m% e6 V public int hashCode() {4 _: r* g0 |5 }2 x& P
return Objects.hash(name, age);: i7 O! E; f8 L& v: B# I% T
}, d( c$ D2 r+ e
}+ z8 k: L$ c& m- ]" O( D
17 |0 D6 a& M+ Y4 \
2 ! b: V0 I- w/ w, Y7 l9 f' D3, c1 G/ t0 w) f$ C- h) o; U
4 " C8 [3 M1 a1 {/ A( Y8 {" ^5 & Q( ^$ R2 {, e0 u. F3 H9 A8 r$ c6 0 P: p: B* { Q; O72 }# R& n8 C- J4 a% w; E
8 ; D+ H( f& V+ U# ]8 b& \5 r6 j96 J/ [# ?3 ~' |6 q8 A1 z+ T
10 $ v8 ^7 Y; l3 n5 g* S11 + T! R1 U4 Q; @5 x12 & w/ g" Q+ e Q8 F# ]: X3 w8 V138 o) F) D% | u# ]/ s0 A1 }
14" a H* L) J5 f! }! ]1 \
152 k5 |6 V; ]; X
16 - U+ R6 D% u1 f$ i17% g% V+ w. [% K: b/ f
18 / L6 S r i0 h; q19 ' J$ @# y, T8 Z j0 ]20 9 ^. V, V+ a, Y, m7 E3 E21 / P2 m, z( P& I; @22 3 i' @8 \ K4 R2 f: C* p2 T23 1 o! |- I$ |% `$ i+ c24' N; k6 q! l D* f
25 9 y: k* v6 W, {+ c% N" [2 M267 i& U$ j1 _- R* S+ s. M
27 8 K; a1 @% j6 H6 ^7 J& c* t/ j1 B28# L8 R8 B; E9 i( }' m6 I
29 3 N. s% O6 u+ _ \9 u+ K) X30 0 Y0 ^% S. K/ D& j3 N, f- C/ e31 / ]2 H0 C- I; a1 Z32 2 g3 N$ v6 |' c: l. {" i" z2 Q6 l33 . ~9 H& z, D& B2 U34% K7 C9 L; n' C' ]: d+ k0 H& o1 b
35! W* l, P- Q2 |$ V* l
36 - X5 k4 ` t5 X1 h37% b/ z' T& G; [8 c* d$ J3 C$ `* N
38 4 y. S+ x, x+ w$ v _$ z$ C9 `) y398 W- h! j0 X1 r" k+ \9 \
40, l) F8 Z0 t. w8 }8 {
41 / b) H9 k- ^5 [8 g# m ^; p6 ]42# E N6 |4 I! T, x+ S7 Q8 f" U
43 % p# N3 p# [" T8 a* G* u/ \( B2 J44& M) i7 K" K' ]% E( }8 M8 `; E
上述代码测试了,必须重写 equals 和 hashcode 方法,才能正常作为 map 的 key,如果有其中一个方法没有重写,都会造成 “hello” 不打印。 ; K, r# A! f% d! Y1 m+ L& O* H1 ^$ i- R$ ~
clone 方法 - {* h/ t* c5 q- G3 \6 }5 H8 X" c R# F
protected native Object clone() throws CloneNotSupportedException;+ R$ E. C0 m5 R% K7 T
1 * G3 Z' S% A- ?) s& ?观察源码可知,clone 方法是 protected,子类只能调用受保护的 clone 方法来克隆它自己的对象,即只有在 Person 类中才可以克隆 Person 对象 3 K# k9 `. s; I! m 9 _: w! j4 Z* ?. opublic class CloneTest { % k% h9 g9 N6 P9 V/ ?# I8 ^; y( K public static void main(String[] args) { ?; M& L4 t" ]
Person p = new Person();; c- r* d. u$ \* ]1 `4 P
Person p2 = p.clone(); // Error clone() has protected access in Object! @/ B y) L0 X8 d
}8 W3 x- |: r6 B" Z. x
}4 i, t: E6 s; H8 N/ i) v
2 `9 a" t# |5 i
class Person { 9 H4 v9 K: N1 w" k' G5 L; t" P private String name;. k5 t3 S' g: f' Q
private int age;" R7 m: o# t& [% K" w6 `/ r/ M
} % A; t* x2 {# h5 O1 : ]( ~- ?) W' L X2' S0 M" h9 R4 _2 c) N7 K: U4 _/ R
3 8 I" ]* x/ l+ J& k! n5 y4 . Q9 n9 @8 Y F7 g8 O57 \; z2 ^/ m- Q2 g9 q+ H
6 9 m4 F& M% E& G9 ^8 e72 M# S1 O9 M, F1 N& F9 ]& F' g8 T
8 8 n) N5 t+ q7 s/ B9 / \; }+ D! F$ l* a" x, k! b* f10 ( ~* R1 K+ R6 \' D" V4 ^2 E [, O' b11 . N1 N3 O) E P7 {我们可以重写 Person 中的 clone()方法为 public 即可调用. l& z9 g0 F3 A; A: T
7 A) w" v) p0 ]* a' E5 w
public class CloneTest { I) f7 G4 f1 Y4 U$ Q* b; O8 v
public static void main(String[] args) { ) G* l3 ?; d S0 c, W' E( _% c Person p = new Person(); * S; L1 F3 _% u% }! l try {- y2 E3 y# L5 X$ y7 X. O& [6 x L, d
Person p2 = p.clone(); . V- o1 v/ ^ v" } } catch (CloneNotSupportedException e) {8 H" g2 z8 F% f5 Y
e.printStackTrace();6 b1 |+ c8 w, q0 o) Y8 } j
}8 N' p# x4 D v+ v
} 2 E* I& { N7 P/ O* @}: D5 b$ Z' v* i$ q+ s
3 a" P- V0 ^8 i" n( W# {' i) Jclass Person { $ `* f: J7 O: t. c$ q7 Y private String name;2 [" V' i* a2 v3 p( d) y7 W: d
private int age; / E1 i2 x+ x' v8 y- f. s, D. N7 O0 h. S( v! W0 j( e
@Override 1 v5 Y/ o' [. w# c7 N% S; C protected Person clone() throws CloneNotSupportedException {" x+ F5 V+ \1 {- V a; ]
return (Person) super.clone(); " B( |% ]' \9 b9 z8 J0 [; E }1 r7 t) W/ V7 g% ^: E
} - w* q8 Q: H! X9 ]0 }1: V' O+ Y5 j$ ?& B4 @
2" ]2 S) K/ a4 H
3 ) t* I: l8 u$ s& _. w: d: {4. G/ G6 B2 G4 l _; c
5 ! E& U1 u6 @0 y; ]( g( l6 - Q+ P) o) D- i$ B a7+ W. Z# u! V" L8 _; \
8, D! v- ]. |' T: E4 l; m* r
9 K2 O0 q+ J% w" Q9 }
10" | R4 N4 W t& l+ G$ y
11 % N# | g% }4 V, b5 @12 ; x" y+ x/ N# K0 C+ P9 J: f13 1 L5 \0 t& \1 }3 y6 r! }14 9 O) }. r( h u1 |0 Q3 s) f j8 S$ P2 N15 , s+ `2 s7 A" Y2 w7 O# K) e& J16& r8 U: o/ ~2 O! m* S2 p' `
172 u" c: { d7 N
182 W" x; X! j$ ~1 E8 `
19 b- i. g- |" w3 b8 X20 ?0 X; d4 N3 n# v& I但是当我们执行代码时,还是会抛出运行时异常:java.lang.CloneNotSupportedException,这是因为我们没有实现 Cloneable 接口,这个接口是一个标记接口(marker interface),只是作者了解克隆过程。 ; E d& o1 Y* S4 V2 f, g 5 n! V# {* E1 z& _+ O$ Q: ?. Z: v" r' M浅拷贝; J) S8 O' K+ J7 B% i, _2 ~
: h. B8 p! o( f
使用默认的方法得到的拷贝为浅拷贝,即拷贝的对象与原对象中的引用变量指向的是同一个 5 ]7 r! X: f+ Z: s% E& p- f ) H/ V6 z. N2 [+ m/ f6 q! Mpublic class CloneTest { - L" z& ]5 |' N9 H4 f0 C public static void main(String[] args) { : [5 J2 U3 m4 h( j( D Person p = new Person();" w( \/ d! p1 s8 G5 _
try {6 w$ `' v8 n. Y1 n
Person p2 = p.clone(); 9 t9 z8 G. H. A8 W0 n) J5 h System.out.println(p.get(0)); 6 r: B6 D- O+ W, @" B0 {! O+ @ System.out.println(p2.get(0)); # Q/ s2 ~3 o U$ d$ w$ c p2.set(0, 8); 1 }7 a- C8 W: b$ H: B7 @/ m System.out.println(p.get(0)); - D! W! z5 b+ ~8 n/ [ System.out.println(p2.get(0));" W# i2 _& |/ K2 K' Y p
} catch (CloneNotSupportedException e) { ' n! T% x4 U9 u# s- L# T7 i s e.printStackTrace(); ; S+ m+ v, s* s( F9 S4 @* R7 k } ' c. D/ X& W+ U% ?0 F }6 f6 ^/ V' A& q' }
} $ R' }& u0 l1 Y8 l# o, p7 n 4 c8 F9 ?3 {1 f* X! E7 Bclass Person implements Cloneable{ 2 a# _7 p4 D8 a( z, m private String name;( P( y$ @6 E$ G, t0 [
private int[] nums; & e5 s, s1 }4 V- ~! J' M A" V( d" s7 b- Y/ R
public Person() {2 q. J" W- ~% X
this.nums = new int[]{1, 2, 3};- z Y7 w$ ^, p& d! V
}! a+ S- L, q4 c# L
6 d) l, f3 q' g6 |
public void set(int index, int value) {* a9 [% D& j2 S! g; N0 e
this.nums[index] = value;, F9 ~2 |6 G. f) P3 U
} w6 q# a# s- N$ X- Q { ! t# w" | z7 k" f. M% L public int get(int index) { * n5 o X# W8 Z& Y2 t# r return this.nums[index];/ v6 l( l' |2 K
} Z/ D& C" }: ?9 }1 _
0 M, ~7 d* a& w/ D
@Override# L8 T$ {' ]* H
protected Person clone() throws CloneNotSupportedException {6 Z* d2 w- @. f( |
return (Person) super.clone(); 5 O6 S# H% H2 g2 @( j9 p } 4 y! ]0 T9 n- _2 @5 ?: Q} 0 p, s+ c1 M+ F$ p9 X; j ' y$ O6 S1 Z. ^- v) I% x8 g// output; o0 {- W) f4 x3 N; k2 g
// 1+ l, Q+ _/ {6 f$ j( R: S6 n
// 1 * x0 Z6 v x; @) k# c. Q& k/ N" Z// 8- Z3 t5 @7 H( D, f% B w
// 8 ) S8 `. T4 Y( z4 F; P3 j17 H$ ] B z* L
20 B/ w, ]$ X7 E* o* ^- }6 y. a8 B
3; U# b+ l" I6 ^' J
4 0 u/ n+ M) N) y s X5 $ }& u+ u2 X& Y1 H" S0 h6 # j6 ]: z1 [' D* f I$ a2 c7 $ S6 I# e: M8 R% B. x% ?8 & Z2 |, Z5 r2 x6 O, G* p5 r98 O+ @2 v6 i2 z% ]. N5 o" t
10. R$ s# ^" x, a; S) T; X
11 - Y+ e5 i2 K( L4 t) j E12 ! E5 a$ T% j' N3 ?( \8 a13 5 _, _' X' A; K8 O) h9 c145 g1 Q7 w3 m3 g: X* ~8 @
15% s7 S9 e# o; L6 K( ~
165 ]! V* o- J9 K, d4 V& i8 d1 [
17 ' p) Q, |/ \! [+ O18 6 |. t0 ]# J6 z6 |197 c! f) i' b* T) }' G ?- f) |. X
20 0 n0 {: E8 m+ ^5 y2 N, m/ E# v21 : Z$ t) r% }( @' e22; H4 p: T. v/ O7 x- N( ~6 @# w' t& c) @ M
23) P& p$ F: g5 \. J5 z6 [
24 % p, p9 j1 H& w" T25 # l& b8 `* e" W1 y' q! L5 _26 $ k2 B/ j8 N) m* A27 3 g: M1 Z; r$ ~ G8 y280 `; f" p: V4 @9 [' M* G
294 {( I1 R8 u0 M: j
30. p. v+ G$ d7 H/ H
31; `6 |- ~. P/ M+ @, t) \
32/ G$ \1 \+ s7 E$ {+ i8 c' s+ `+ U8 j
33) j' Y7 C: | W/ F% Q' i. Q
34 / }, b3 l( |/ a' @8 [ Z35 2 h# l1 H5 f: t5 ~$ M8 y1 Q) a364 g+ a4 x4 j* t
37 " z8 e7 m/ ]$ U* R3 l387 V" W( s1 O+ r5 F0 Q* s! G
39 - K6 e! M) h. T: k" {40; O2 [$ y' L0 ~$ j: e y
417 b. g* {- j6 m4 ?
42 - u. I4 Z) g3 k% b43' p4 t2 d3 ?0 k
深拷贝 + p% z4 A* ^% I; A . v) \# i+ e" h n& h9 c' N为来解决上述问题,我们可以使用深拷贝6 T0 {; ~2 s1 o( G4 w, E, \: z# c: ]
$ w" m3 c5 p( Rpublic class CloneTest {7 \5 @3 K9 e/ x" B
public static void main(String[] args) { " o% \1 |0 U: u$ ?4 y Person p = new Person(); 4 {. c8 S6 }/ l+ L0 ]8 a4 E& |. t try {$ V" v& n" |+ \. o
Person p2 = p.clone(); 0 n1 D: q! W4 a2 H9 D System.out.println(p.get(0));1 W* b! w7 x/ X! D% b
System.out.println(p2.get(0));& C& R& w) g! {% S. K- }& {
p2.set(0, 8); 8 A& t0 x1 t' X) | System.out.println(p.get(0)); ; G) D. M: \ i1 k3 ^ System.out.println(p2.get(0)); , a3 ^2 _- R& x. b3 z4 k } catch (CloneNotSupportedException e) {7 m- Y. Z( `1 v) |' P
e.printStackTrace(); , ~6 c0 k9 z/ b0 D } d( M( t/ [4 f7 b" l
} + [) D" W- m2 D0 F: b v} - u+ j) r' }! g8 A) r' P # ^/ N, |) P5 P. I& Hclass Person implements Cloneable{ % l9 K5 J/ f6 K8 O6 y$ X private String name;1 x( T2 l' g8 w( A( q! \
private int[] nums;9 z1 x5 |. A; |5 i4 o" H: s: H
% B' K0 B% W8 K# W5 d# _, w, q
public Person() { ; g0 r: j( j( i, c this.nums = new int[]{1, 2, 3}; 1 k; A1 b9 n4 y* U) K' y } ( [% Z' f" E! |. u) b" _( } Z1 x4 M: N& @, _9 f |# v public void set(int index, int value) {. G& O- W2 u9 w8 u
this.nums[index] = value;& a; U; I, ?1 j3 J0 s! E# Y6 u
} " v; W& d9 Q: u $ r- p; ~1 |9 _0 O! U public int get(int index) {$ f! `7 A# |4 w$ x* O! W3 G6 ^0 k
return this.nums[index]; , k# c# N i$ {5 i6 a) n1 s. Q! }" ^ } ) G o0 Q" Z5 ~( C3 x* z( F% Y' B 8 Y( }0 q* ]& E2 G4 n) q @Override; `+ x& _+ U+ U0 X$ s7 Y1 ^; G
protected Person clone() throws CloneNotSupportedException { 8 Y* x5 Q% {- ~+ O+ b& I Person p = (Person) super.clone(); 1 l, y( d3 A7 q, d p.nums = p.nums.clone(); // array clone2 w1 p# m) }. `! u
return p; 1 n Q1 {3 r) s3 r# E c } 4 n+ w, }; U, u R" R} 4 m+ o0 l* M s' u% o; U/ Y0 [1 C4 c2 V
// output 6 Z9 ?' q2 r% y g D; k6 m// 1 . B% Y5 R5 h: @( n// 1+ [- K3 ?2 L/ C/ M
// 1( h5 X4 O+ y% u# X: n+ a8 G
// 8 3 W X( `! b% S2 A1 + d7 Y }: t8 m; M+ d0 P# ?2 ( d; }. x- t& `5 c8 n9 L+ y3 4 g# I2 |2 @9 R4 / V7 k! i; c$ B0 a5 / H% b$ H. E4 j% F, x6 / ?+ [$ ]$ B" [' L: j7( m6 _& u- K) ~' u. B" N" O
8, P$ Q2 c7 a3 _
9$ y" m& I( t5 d q, X! t
109 k' j/ K8 q/ e9 M
11+ g; O% E* r" g2 g
123 ?: S4 y! {2 g3 \ y- }- H0 q
13: o* s2 ]8 ?5 L9 f' N" |% A
14 4 W, J. ]8 ^* W. `' C15) n* R0 N9 g' I
16 " S3 K" j! f; I9 n! l5 L- z5 Z: V17 2 Y9 ~% @$ t/ w2 M! _3 h/ c18 ' g9 h9 t7 q$ t* @# y! p* k* }19 + P( [! o6 ]5 b' v20 3 v3 T, ]& K% |% V21* y8 J2 b( i0 B
22 9 _; X( h' W, j7 L23' b8 Z6 R6 ]7 | A# W. Y; |
24 ( [7 L" B: o! |/ G* i! s25 x% O" n# E* a" g2 t5 K! U, ~26 2 S7 x: S, _/ e; e+ b/ p* G; y# @9 G27 5 S, i% g* G6 C. W, x* W! i, u28 1 t0 p' g/ x& m/ Z1 u3 J29 ( N( Y2 i7 G/ k& k- k$ Q30" X2 f7 B4 A& R+ G5 `9 Z0 C5 _
31- G$ B' j b6 u. X$ e3 O, G
32 7 ~( {/ p0 j5 D2 s33 6 V6 P$ v0 A/ \0 p34+ J. l" X& u. ~) c7 S- w/ R
35& g% B% p! K+ X5 I
36 " }/ u9 E! y! q. t376 e3 E8 Z8 T1 h; M8 x( d8 Y+ e
38 + Z8 R2 B& @ u; {6 }$ [1 U+ N' l' M g$ s39 $ M8 E* l5 `& }. ]3 a" S408 T+ Z$ C$ u2 I% m
41- c% `* b k* o
425 X& T9 Z c& g
43 ' n8 P- ^! ^6 x44 & V5 ~) H. |5 O. Y45 9 J v( v2 |, _注意,数组的拷贝可以使用数组自带的 clone() 方法,该方法为 public,所以可以直接使用。若有其他引用类型变量需要拷贝,可以使用该类型的 clone 方法。( j9 v# p3 r. c: G! A; B" _7 ~0 W
0 }1 j6 m# R: hclone() 替代方案 " q! |7 r2 g/ e* ^2 v7 d" j, f5 F& W$ D
clone 使用频率一般较低,我们可以使用拷贝构造函数或者拷贝工程来拷贝一个对象: " D! W$ r$ l3 A( t: e. X1 U% f" R/ L0 ~
public class CloneTest {0 n: @" y2 ?, G8 t4 P
public static void main(String[] args) { # H( m! a' {; Q! D ?9 ~ Apple apple = new Apple(); & i# Q. n1 p* D1 o1 [ Apple apple2 = new Apple(apple); ( k; i- G* b) A. s$ s6 ]3 v System.out.println(apple.get(0));) B) T6 l3 z1 \
System.out.println(apple2.get(0)); . U7 F4 u6 a" R" z8 {0 A, | apple2.set(0, 8);: ]1 }8 H. z9 N. Z
System.out.println(apple.get(0)); ' @3 H1 G9 q: s4 q7 h7 } System.out.println(apple2.get(0)); ' c+ ]; s. q' K0 I% g2 y }# c! x9 V3 K5 Z( t9 ]
}$ M$ d, i4 B1 W) _4 |
class Apple {% l5 L; D4 b, D# v
private int[] nums; ( }0 H; u# @9 L" j- O. j0 X4 ] private String name;; w+ y% a9 q5 ?: Q7 p
% H& w( `. Y$ u6 Y' ]$ u* } public Apple() { ! U6 A1 {0 ^& \4 M this.nums = new int[]{1, 2, 3};( V4 q! p: M. s/ s. `" J6 [2 n' R
}5 T. U- V; ^; Q5 _( a2 t
2 w, M( v/ C/ ~1 a5 x5 s$ Y( e' K
public Apple(Apple apple) {' ~9 x5 W/ m" n5 I& V
nums = apple.nums.clone();0 }% g5 W+ Z! Z% \" Q; g# `
name = apple.name;% w+ c5 ?" V$ ]* N$ e
}, g% p7 [; w* v/ s* r" Z5 @ s0 ^
0 _8 C- w5 T m1 S
public void set(int index, int value) {6 O5 v1 q8 t) Q/ u5 @3 V8 V
this.nums[index] = value;$ y/ r; Z6 A9 q( q+ ~3 j/ d
}. F1 e- m$ G6 ^( ?. U3 y
: K: p1 {8 S. p, w public int get(int index) {9 E7 ~$ A* J- a4 a; A5 T
return this.nums[index];! J) v( L; S) B+ i( ~% W
}6 i! x; y. i" d \3 D
}. G4 o" S; W7 p0 X
. K/ K9 Z6 E. a, D* k// output 1 d3 `/ k* \4 s& h* K) V// 1 0 s7 P# J0 R: k6 T4 C/ {/ S+ i// 1 / d- X: q( T: {7 Z3 I, x' Z! r: f// 13 {% C5 u9 [5 P6 W: `
// 83 ~) q7 n: c4 E4 h J K$ V1 I
1 e, q. t' |# i: j& l; E/ Z- j% f
2$ N# Y+ `; r6 w6 }# A
35 {; M2 B" C C
4, i; P- [6 V) n* J' h% M
5/ q! n7 \: c% H% K, g7 g
6, y" X( l0 E, D5 q3 Y
7 " z8 Q+ m" |; ]6 h7 _# s8 , T: n5 Q0 O4 e. a. ?! p z5 i94 i( f, |# O$ E# k
10 7 E d% @4 Z4 T" B/ f( I# o11. A+ w, u- `. o7 k$ C
12* R3 z! N3 e9 }! j% n" n7 {
13( o. L7 O h0 ~6 z0 Y( `
14 ; ]- z% J( L6 }" a# s" h150 i2 x6 L2 h1 i" i$ l/ Y) _
16 . Q3 l+ H4 e% M, D3 R0 w. q! N17( I/ f Z9 h' V
18 $ a7 C/ t% r' r+ D# _19 % M6 `0 d7 N2 @' y. o7 \0 v205 T+ X0 {# v* u2 T, G& `
21 " e: ?% c# k9 V& i2 R3 s" L22( g6 _* k+ f& x- @
23 ! @. r2 t4 M/ H, O24 ( l4 ^3 P9 }! z1 J252 P; _! c' h$ h* {
26" Z5 X& T T% D. S3 l$ A; r; m/ L
27+ O1 ]; U/ v, M# q2 z8 A; ?
28 - x1 ^, J3 G7 m- Q& y297 V# i5 V: J/ K) u
30 5 G# l. n1 }' O( y+ Y4 p31 0 n: z9 {- Q9 }4 u1 ]32$ l8 Y3 Y8 e3 {8 z2 D* g) Y6 H
335 R& b5 |; U) D* E g5 H' T. A, @$ D
34: q* C4 n8 Z- b* ~
35( J9 A9 D. G' B- s0 X
36 4 t5 `8 G7 b* V$ |: K1 M7 R37: A8 B/ Q2 w8 }
38 " u8 _/ V$ F* j yReferencee, r; \2 ]" S& h# u
) ]4 D& s8 ] h1 g' d3 d
Java核心技术卷一" D# \0 e3 e* Z8 o
+ h8 L3 d L' T6 u) V
Class Object9 Y* }5 ^+ A6 R+ j- t8 |' B
3 m) i. i+ A. G6 F
Equality, Relational, and Conditional Operators# p" i: G: t0 y2 q" C7 j
3 L* }5 @) e6 e5 Q, n9 t8 Rnative keyword in java" R( U- p B- }2 G
————————————————2 _2 G" d+ W# [# T6 b6 |$ x
版权声明:本文为CSDN博主「EJoft」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。. o3 q( x5 C' |7 Q- E3 K
原文链接:https://blog.csdn.net/EJoft/article/details/106012278 5 c# G' r: t4 D) @0 D( ^7 [) b7 k- E( l9 N$ d3 \