1
2
3
4
5
6
7
|
Person p =
new
Person(
23
,
"zhang"
);
Person p1 = p;
System.out.println(p);
System.out.println(p1);
|
1
2
3
4
5
|
Person p =
new
Person(
23
,
"zhang"
);
Person p1 = (Person) p.clone();
System.out.println(p);
System.out.println(p1);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public
class
Person
implements
Cloneable{
private
int
age ;
private
String name;
public
Person(
int
age, String name) {
this
.age = age;
this
.name = name;
}
public
Person() {}
public
int
getAge() {
return
age;
}
public
String getName() {
return
name;
}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
return
(Person)
super
.clone();
}
}
|
1
2
3
4
5
6
7
8
9
10
|
Person p =
new
Person(
23
,
"zhang"
);
Person p1 = (Person) p.clone();
System.out.println(
"p.getName().hashCode() : "
+ p.getName().hashCode());
System.out.println(
"p1.getName().hashCode() : "
+ p1.getName().hashCode());
String result = p.getName().hashCode() == p1.getName().hashCode()
?
"clone是浅拷贝的"
:
"clone是深拷贝的"
;
System.out.println(result);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
static
class
Body
implements
Cloneable{
public
Head head;
public
Body() {}
public
Body(Head head) {
this
.head = head;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
return
super
.clone();
}
}
static
class
Head
/*implements Cloneable*/
{
public
Face face;
public
Head() {}
public
Head(Face face){
this
.face = face;}
}
public
static
void
main(String[] args)
throws
CloneNotSupportedException {
Body body =
new
Body(
new
Head());
Body body1 = (Body) body.clone();
System.out.println(
"body == body1 : "
+ (body == body1) );
System.out.println(
"body.head == body1.head : "
+ (body.head == body1.head));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
static
class
Body
implements
Cloneable{
public
Head head;
public
Body() {}
public
Body(Head head) {
this
.head = head;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
Body newBody = (Body)
super
.clone();
newBody.head = (Head) head.clone();
return
newBody;
}
}
static
class
Head
implements
Cloneable{
public
Face face;
public
Head() {}
public
Head(Face face){
this
.face = face;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
return
super
.clone();
}
}
public
static
void
main(String[] args)
throws
CloneNotSupportedException {
Body body =
new
Body(
new
Head());
Body body1 = (Body) body.clone();
System.out.println(
"body == body1 : "
+ (body == body1) );
System.out.println(
"body.head == body1.head : "
+ (body.head == body1.head));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
static
class
Body
implements
Cloneable{
public
Head head;
public
Body() {}
public
Body(Head head) {
this
.head = head;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
Body newBody = (Body)
super
.clone();
newBody.head = (Head) head.clone();
return
newBody;
}
}
static
class
Head
implements
Cloneable{
public
Face face;
public
Head() {}
public
Head(Face face){
this
.face = face;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
return
super
.clone();
}
}
static
class
Face{}
public
static
void
main(String[] args)
throws
CloneNotSupportedException {
Body body =
new
Body(
new
Head(
new
Face()));
Body body1 = (Body) body.clone();
System.out.println(
"body == body1 : "
+ (body == body1) );
System.out.println(
"body.head == body1.head : "
+ (body.head == body1.head));
System.out.println(
"body.head.face == body1.head.face : "
+ (body.head.face == body1.head.face));
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
static
class
Head
implements
Cloneable{
public
Face face;
public
Head() {}
public
Head(Face face){
this
.face = face;}
@Override
protected
Object clone()
throws
CloneNotSupportedException {
//return super.clone();
Head newHead = (Head)
super
.clone();
newHead.face = (Face)
this
.face.clone();
return
newHead;
}
}
static
class
Face
implements
Cloneable{
@Override
protected
Object clone()
throws
CloneNotSupportedException {
return
super
.clone();
}
}
|