1.关于new:当程序执行到new:(两个对象的引用s和s1指向的是同一块内存空间)
首先看new后面的类型。
然后根据类型分配内存空间。
之后调用构造函数,填充对象的各个域(对象初始化)。
最后把她的引用放到外部,在外部通过引用操纵对象。
2.关于clone:当程序执行到clone():(两个对象的引用t和t1指向的是两块不同的内存空间)
首先分配内存空间,分配的内存空间和原对象相同。
使用原对象中对应的各个域,填充新对象的域。
填充完成之后,clone方法返回,一个新的相同的对象被创建,
把这个新对象的引用发布到外部。
package com.bawei.yjs.newAclone;
public class NewStudy {
public static void main(String[] args) {
Student s = new Student();
Student s1 = s;
System.out.println("s:"+s);
System.out.println("s1:"+s1);
System.out.println("=============================");
Teacher t = new Teacher();
try {
Teacher t1 = (Teacher) t.clone();
System.out.println("t1:"+t1);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t:"+t);
}
}
===============================================================
s:com.bawei.yjs.newAclone.Student@70dea4e
s1:com.bawei.yjs.newAclone.Student@70dea4e
=============================
t1:com.bawei.yjs.newAclone.Teacher@5c647e05
t:com.bawei.yjs.newAclone.Teacher@33909752
=========================================
public class Teacher implements Cloneable{
private String name;
private Integer id;
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
public Teacher(String name, Integer id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
=======================================================================
package com.bawei.yjs.newAclone;
public class Student {
}
=================================================================
3.关于浅clone:
public class Person implements Cloneable{
privatint 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();
}
}
===================================================
Person p = new Person(23, "zhang");
Person p1 = (Person) p.clone();
String result = p.getName() == p1.getName() ? "clone 是浅拷贝的" : "clone 是深拷贝的";
System.out.println(result);
================================================
关于深clone:
如果想要深拷贝一个对象,这个对象必须要实现Cloneable接口,实现clone
方法,并且在 clone 方法内部,把该对象引用的其他对象也要 clone 一份,这就要求这个被引用的对象必须也要实现
Cloneable接口并且实现clone方法。
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() {}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
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));
}