文章摘自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操作,重新复制了一遍。
相关推荐
本书《Diving Deep Into Kubernetes Networking》介绍与Kubernetes相关的各种网络概念,对于操作员、开发者或决策者可能会发现有用。网络是一个复杂的话题,尤其是当涉及到像Kubernetes这样的分布式系统时。了解技术...
Web API 支持RESTful架构风格,允许开发者创建可被各种平台和设备访问的服务。Akhil Mittal 撰写的《*** Web API》是一本深入探讨如何创建.NET平台上的RESTful服务的实用指南。Akhil Mittal 是一位资深分析师和微软...
Contents Introduction ................................................................................................................................... 3 Meet the Authors ..............................
图深度学习研究库DIG:Dive into Graphs是图深度学习研究的统包库。 DIG:用于图深度学习研究的总库,刘萌*,罗有志*,王丽梅*,谢耀辰*,郝昊*,桂树瑞,赵旭,余海阳,张静屯,刘毅,严克强,宝拉·奥兹特金(Bora...
broad technical audience, allowing beginners a gentle start while diving deep into advanced topics and showing how to build production-ready systems. In this book you will learn how to: 1. Get up and ...
You’ll begin by working through some basic examples in TensorFlow before diving deeper into topics such as neural network architectures, TensorBoard visualization, TensorFlow abstraction libraries, ...
在阅读"Diving into the Laravel Framework"这本书时,你可能会学到如何编写单元测试来验证WAL接口的正确性,确保在事务处理过程中数据的完整性和一致性。Laravel的内置测试框架`PHPUnit`可以帮助你编写这些测试,并...
This book is an end-to-end guide to TensorFlow ...broad technical audience, allowing beginners a gentle start while diving deep into advanced topics and showing how to build production-ready systems
You’ll begin by working through some basic examples in TensorFlow before diving deeper into topics such as neural network architectures, TensorBoard visualization, TensorFlow abstraction libraries, ...
beginners a gentle start while diving deep into advanced topics and showing how to build production- ready systems. In this book you will learn how to: 1. Get up and running with TensorFlow, rapidly ...
You’ll begin by working through some basic examples in TensorFlow before diving deeper into topics such as neural network architectures, TensorBoard visualization, TensorFlow abstraction libraries, ...
深入了解指令 看演示 官方文档: 一种主要在指令中使用控制器的有趣方法: 更多关于 bindToController: 用于构建此演示文稿的技术: 作为展示框架 用于代码高亮 用于在开发时编译 sass 并捆绑类似 livereload 的...
Diving into the shallows: a computational perspective on large-scale shallow learning. In NIPS, 2017. 当最小化线性和核最小二乘时,它可以加速SGD迭代的收敛,定义为 待办事项:更新自述文件 带标签的训练...
In the later chapters you will be deep diving into the different aspects of SMACK. You will be starting with functional thinking and problem solving using Scala. You will understand Akka architecture...
You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...
You’ll learn how to create something visible with Rails’ view layer before diving into the more difficult inner layers: the database models and controller code. All you need to begin your Rails ...