谨慎重载clone方法

public class Car implements Cloneable { private Object containedObj1 = new Object();
private Object containedObj2 = new Object(); public Object getObj1() {
return containedObj1;
} public Object getObj2() {
return containedObj2;
} @Override
protected Object clone() throws CloneNotSupportedException { return (Car) super.clone(); } public Car() { } public static void main(String[] args) { try {
Car obj1 = new Car();
Car obj2 = (Car) obj1.clone(); System.out.println("obj1 and obj2 are same:" + (obj1 == obj2));
System.out.println("obj1.containedObj1 and obj2.containedObj1 are same:" + (obj1.getObj1() == obj2.getObj1()));
System.out.println("obj1.containedObj2 and obj2.containedObj2 are same:" + (obj1.getObj2() == obj2.getObj2()));
System.out.println("obj1.str and obj2.str are same:" +(obj2.getString() == obj1.getString()));
System.out.println("obj1.data:" + obj1.getData()+"; obj2.data:" + obj2.getData());
System.out.println("obj1.dataf:" + obj1.getDataf()+"; obj2.dataf:" + obj2.getDataf());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} }
}

public class Car2 {
private Object containedObj1 = new Object();
private Object containedObj2 = new Object(); private String str = "oneStr"; private int data = 1024;
private float dataf = 1024.1024f; public Car2() { } public Car2(Car2 car) { this.str = new String(car.getString().toString());
this.data = car.getData();
this.dataf = car.getDataf(); } private String getString() {
return str;
} public int getData() {
return data;
} public float getDataf() {
return dataf;
} public Object getObj1() {
return containedObj1;
} public Object getObj2() {
return containedObj2;
} public static void main(String[] args) { Car2 obj1 = new Car2();
Car2 obj2 = new Car2(obj1); System.out.println("obj1 and obj2 are same:" + (obj1 == obj2));
System.out.println("obj1.containedObj1 and obj2.containedObj1 are same:" + (obj1.getObj1() == obj2.getObj1()));
System.out.println("obj1.containedObj2 and obj2.containedObj2 are same:" + (obj1.getObj2() == obj2.getObj2()));
System.out.println("obj1.str and obj2.str are same:" +(obj2.getString() == obj1.getString()));
System.out.println("obj1.data:" + obj1.getData()+"; obj2.data:" + obj2.getData());
System.out.println("obj1.dataf:" + obj1.getDataf()+"; obj2.dataf:" + obj2.getDataf()); }
}
@Override
protected Object clone() throws CloneNotSupportedException {
Body newBody = (Body) super.clone();
newBody.head = (Head) head.clone();
return newBody;
}
public 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;
} public static void main(String[] args) throws CloneNotSupportedException { Body body = new Body(new Head()); Body body1 = (Body) body.clone(); System.out.println("body == body1 : " + (body == body1)); System.out.println("body.head == body1.head : " + (body.head == body1.head)); }
} class Head implements Cloneable/* implements Cloneable */ {
public Face face; public Head() {
} public Head(Face face) {
this.face = face;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
} } class Face { }
public class Body2 implements Cloneable {
public Head head; public Body2() {
} public Body2(Head head) {
this.head = head;
} @Override
protected Object clone() throws CloneNotSupportedException {
Body2 newBody = (Body2) super.clone();
newBody.head = (Head) head.clone();
return newBody;
} public static void main(String[] args) throws CloneNotSupportedException { Body2 body = new Body2(new Head(new Face())); Body2 body1 = (Body2) body.clone(); System.out.println("body == body1 : " + (body == body1)); System.out.println("body.head == body1.head : " + (body.head == body1.head)); System.out.println("body.head.face == body1.head.face : " + (body.head.face == body1.head.face)); }
} class Head implements Cloneable/* implements Cloneable */ {
public Face face; public Head() {
} public Head(Face face) {
this.face = face;
} @Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
} } class Face { }
public final class Galaxy { /**
* Regular constructor.
*/
public Galaxy(double aMass, String aName) {
fMass = aMass;
fName = aName;
} /**
* Copy constructor.
*/
public Galaxy(Galaxy aGalaxy) {
this(aGalaxy.getMass(), aGalaxy.getName());
//no defensive copies are created here, since
//there are no mutable object fields (String is immutable)
} /**
* Alternative style for a copy constructor, using a static newInstance
* method.
*/
public static Galaxy newInstance(Galaxy aGalaxy) {
return new Galaxy(aGalaxy.getMass(), aGalaxy.getName());
} public double getMass() {
return fMass;
} /**
* This is the only method which changes the state of a Galaxy
* object. If this method were removed, then a copy constructor
* would not be provided either, since immutable objects do not
* need a copy constructor.
*/
public void setMass(double aMass){
fMass = aMass;
} public String getName() {
return fName;
} // PRIVATE
private double fMass;
private final String fName; /** Test harness. */
public static void main (String... aArguments){
Galaxy m101 = new Galaxy(15.0, "M101"); Galaxy m101CopyOne = new Galaxy(m101);
m101CopyOne.setMass(25.0);
System.out.println("M101 mass: " + m101.getMass());
System.out.println("M101Copy mass: " + m101CopyOne.getMass()); Galaxy m101CopyTwo = Galaxy.newInstance(m101);
m101CopyTwo.setMass(35.0);
System.out.println("M101 mass: " + m101.getMass());
System.out.println("M101CopyTwo mass: " + m101CopyTwo.getMass());
}
}
输出结果:
M101 mass: 15.0
M101Copy mass: 25.0
M101 mass: 15.0
M101CopyTwo mass: 35.0
谨慎重载clone方法的更多相关文章
- Java中clone方法的使用
什么是clone 在实际编程过程中,我们常常要遇到这种情况:有一个对象object1,在某一时刻object1中已经包含了一些有效值,此时可能会需要一个和object1完全相同新对象object2,并 ...
- Effective Java —— 谨慎覆盖clone
本文参考 本篇文章参考自<Effective Java>第三版第十三条"Always override toString",在<阿里巴巴Java开发手册>中 ...
- Effective Java 第三版——13. 谨慎地重写 clone 方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- Effective Java 之-----谨慎的覆盖clone方法
1.概述 如果clone方法返回一个由构造器创建的对象,它就得到有错误的类.因此,如果覆盖了非final类中的clone方法,则应该返回一个通过调用super.clone得到的对象.如果类的所有超类都 ...
- 第十一条:谨慎的覆盖clone()方法
一个类要想实现克隆,需要实现Cloneable接口,表明这个类的对象具有克隆的功能. Cloneable接口是一个mixin接口,它里面并没有任何的抽象方法,类似的接口有Serializable接口, ...
- Java 深度克隆 clone()方法重写 equals()方法的重写
1.为什么要重写clone()方法? 答案:Java中的浅度复制是不会把要复制的那个对象的引用对象重新开辟一个新的引用空间,当我们需要深度复制的时候,这个时候我们就要重写clone()方法. 2.为什 ...
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
- Object类clone方法的自我理解
网上搜帖: clone()是java.lang.Object类的protected方法,实现clone方法: 1)类自身需要实现Cloneable接口 2)需重写clone()方法,最好设置修饰符mo ...
- PHP面向对象编程——深入理解方法重载与方法覆盖(多态)
什么是多态? 多态(Polymorphism)按字面的意思就是“多种状态”.在面向对象语言中,接口的多种不同的实现方式即为多态.引用Charlie Calverts对多态的描述——多态性是允许你将父对 ...
随机推荐
- vsftpd:非常安全的ftp服务端程序
主程序:/usr/sbin/vsftpd 主配置文件:/etc/vsftpd/vsftpd.conf CentOS 6 /etc/rc.d/init.d/vsftpd chkconfig vsftp ...
- WebService(二)
使用eclipse开发webservice的服务器端以及客户端的简单实例 1.服务端 在eclipse中像建立一个web项目一样,new->Dynamic Web Project A.建一个需要 ...
- brush
简介 Brushing是一个通过点击或触摸来选择一个一维或二维区域的交互操作,比如可以通过点击鼠标并移动. brush经常被用来选择离散的元素比如散点图中的点或桌面上的文件等.它也可以被用来放大选中的 ...
- Delphi下使用指针的简单总结
由于最近公司太忙,好久没有更新我的BLOG了.原来想着写写关于HOOK驱动的文章,可是最后想想好久已经没有做驱动的东西了,怕写出来有错误,于是作罢.开发游戏也有一段时间了,发现使用DELPHI来开发网 ...
- JMeter脚本增强之参数化
JMeter测试脚本录制或者编写,在Web应用和App上的操作方式可能有一点点区别(其实也差不多,哈哈),但是当脚本录制好了之后,对测试脚本的强化,包括参数化.关联.文本检查.集合点设置,甚至再往后的 ...
- [codeforces464D]World of Darkraft - 2 概率期望
D. World of Darkraft - 2 time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [JSOI2009]电子字典 hash
题面:洛谷 题解: 做法....非常暴力. 因为要求的编辑距离最多只有1,所以我们直接枚举对那个位置(字符)进行操作,进行什么样的操作,加入/修改/删除哪个字符,然后暴力枚举hash判断即可, #in ...
- 【BZOJ1068】压缩(动态规划)
[BZOJ1068]压缩(动态规划) 题面 BZOJ 洛谷 题解 比较简单的\(dp\) 设\(f[i][j]\)表示当前已经匹配到了原串的第\(i\)个位置,上一个\(M\)在第\(j\)个字符之后 ...
- Linux正确的关机方式
本人还未入门,仅看书所得. Linux不建议的是直接关电源.Linux后台可能有多人在工作,直接关电源可能造成文件的毁坏. 正常关机之前应该干两件事:一.查看一下谁在线:二.通知一下别人啦,通知别人可 ...
- listen() 函数
声明:本文来自网络博文的合并,文后有链接. 一.listen函数仅由TCP服务器调用 它做两件事: 1.当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用conne ...