Deep diving into Cloning

文章摘自http://idiotechie.com/Mainak Goswami的blog

首先来查看下heap如何分配Object


这并非clone,这只是把对象引用共享性质了。

 

What is cloning?

clone就是把自己给复制所有东西倾囊相授,但是又不是它自己,是不同个体。如同黑哥帝国中所有的黑衣人都是独立个体,但是同是具有同一的能力。

public class CloneStyle {

	/**
	 * @param args
	 * @throws CloneNotSupportedException
	 */
	public static void main(String[] args) throws CloneNotSupportedException {

		Person p1 = new Person();
		Person p2 = (Person) p1.clone();
		System.out.println(p1 != p2);
		System.out.println(p1.getClass() == p2.getClass());
		System.out.println(p1.equals(p2));
	
	}

}

 
 让我们探讨下:

true:代表他们在独立的个体,在内存中分配不同地址

true:代表他们虽然是独立个体,但是他们是来源于同一类型

false:不同对象

 

Shallow Cloning vs Deep Cloning

clone支持Shallow Cloning vs Deep Cloning

shallow只是提供了primitive type的copy,而对于引用类型的Object并不支持



 

 



 shallow cloing的使用主要是通过Cloneable下clone的方法

public class Person implements Cloneable {

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Person o = null;
		try {
			o = (Person) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}

	private int age;
	private String name;

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 Deep Cloning



 

 

public Object clone() {
	//Deep Copy process
		Employee e = new Employee(employeeName, dept.getDeptName());
		return e;
}

 采用new操作,重新复制了一遍。

As an AI language model, I can suggest the following steps to learn deep learning: 1. Learn the basics of machine learning: Before diving into deep learning, it is essential to understand the basics of machine learning, including supervised and unsupervised learning, regression, clustering, and classification. 2. Learn programming languages: To work with deep learning, one needs to have a strong foundation in programming languages such as Python and R, as most of the deep learning libraries are written in these languages. 3. Understand the mathematics behind deep learning: Deep learning involves a lot of math, including linear algebra, calculus, and probability. Understanding these concepts will help you better understand the algorithms used in deep learning. 4. Choose a deep learning framework: Popular deep learning frameworks include Tensorflow, Keras, PyTorch, and Caffe. Choose one and learn it. 5. Practice with datasets: Work with datasets to understand how deep learning works in practice. Kaggle is a great platform to get started with real-world datasets. 6. Read research papers: Read research papers to stay up-to-date with the latest advancements in deep learning. 7. Join communities: Join online communities such as Reddit, Discord, or GitHub to connect with other deep learning enthusiasts and learn from them. 8. Build projects: Building projects is the best way to learn deep learning. Start with simple projects and gradually move on to more complex ones. Remember, deep learning is a vast field, and it takes time and effort to master it. Keep practicing, and you will get there.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值