克隆方法clone()的使用

protected Object clone() throws CloneNotSupportedException
创建并返回此对象的副本。 “复制”的精确含义可能取决于对象的类。

对于任何对象x 表达式: x.clone() != x。只是将类“复制过来”,并不需要重新new ,这在需要很多对象的时候提供了效率上的提高

clone()方法位于java.lang.object,是protected的,使用的时候不能直接调用,需要重写。

重写的步骤是:

1、声明实现Cloneable接口。(贴标签技术,其实质并没有任何方法需要实现,类似于序列化)
2、调用super.clone拿到一个对象,如果父类的clone实现没有问题的话,在该对象的内存存储中,所有父类定义的field都已经clone好了,该类中的primitive(基本数据类型)和不可变类型(string)引用也克隆好了,可变类型引用都是浅copy。
3、把浅copy的引用指向原型对象新的克隆体。

写两个例子

第一个是浅克隆

/**
 * @author 逸川同学
 * 浅克隆
 * 只要数据都是基本数据类型或者String类型,可以克隆一次
 */
public class Client {
	public static void main(String[] args) {
		Person person = new Person("jack",22);
		Person person2 = null;
		try {
			person2 = (Person) person.clone();
			person2.setName("Rose");
			person2.setAge(19);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		System.out.println(person);
		System.out.println(person2);
	}
}

public class Person implements Cloneable{
	private String name;
	private int age;
	public Person() {
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}

第二个是深克隆

/**
 * @author 逸川同学
 * 深克隆
 * 非基本数据类型需要进行深克隆
 */
public class Client {
	public static void main(String[] args) {
		Acount acount1 = new Acount(new Person("Jack",21),100);
		Acount acount2 = null;
		try {
			acount2 = (Acount)acount1.clone();
			acount2.setPerson(new Person("Rose",20));
			acount2.setBalance(200);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		System.out.println(acount1);
		System.out.println(acount2);
	}
}
public class Acount implements Cloneable{
	private Person person;
	private double balance;
	public Acount(Person person, double balance) {
		super();
		this.person = person;
		this.balance = balance;
	}
	public Acount() {
	}
	@Override
	public String toString() {
		return "Acount [person=" + person + ", balance=" + balance + "]";
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	//克隆,进行深克隆
	@Override
	public Object clone() throws CloneNotSupportedException {
		Acount acount = (Acount) super.clone();
		if(person != null){//防范空指针异常
			acount.person = (Person) acount.person.clone();
		}
		return acount;
	}
}

public class Person implements Cloneable{
	private String name;
	private int age;
	public Person() {
	}
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值