文章摘自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,对k8s网络深入介绍,掌握k8s网络原理
国外大牛、微软MVP所著的Asp.Net方面的WebApi书籍
Contents Introduction ................................................................................................................................... 3 Meet the Authors ..............................
Best Programming iOS 11 book Diving Deep into Views, View Controllers,
such as ResNet, DenseNet, Inception, and Seq2Seq, without diving deep into the math. It also shows how to do transfer learning, how to speed up transfer learning using precomputed features, and how to...
抽象wal接口 Diving into the Laravel Framework 一书的示例。 阅读书中的解释。
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, ...
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 ...
Diving into Knockout.JS
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 shallows: a computational perspective on large-scale shallow learning. In NIPS, 2017. 当最小化线性和核最小二乘时,它可以加速SGD迭代的收敛,定义为 待办事项:更新自述文件 带标签的训练...
深入了解指令 看演示 官方文档: 一种主要在指令中使用控制器的有趣方法: 更多关于 bindToController: 用于构建此演示文稿的技术: 作为展示框架 用于代码高亮 用于在开发时编译 sass 并捆绑类似 livereload 的...
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 ...
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 ...