谨慎重载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对多态的描述——多态性是允许你将父对 ...
随机推荐
- js正则表达式匹配斜杠 网址 url等
项目中有个需求,需要从url中截取ID.需要在前台用js匹配截取,所以就百度一下,发现都没有说清楚,所以这里就总结下. 正则表达式如下: var epId=0; //工厂企业ID var urlInd ...
- lintcode395-硬币排成线 II
395-硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 ...
- Java 异常注意事项
异常的注意事项: 1,子类在覆盖父类方法时,父类的方法如果抛出了异常, 那么子类的方法只能抛出父类的异常或者该异常的子类. 2,如果父类抛出多个异常,那么子类只能抛出父类异常的子集. ...
- 奇异值分解(SVD) --- 几何意义 (转载)
PS:一直以来对SVD分解似懂非懂,此文为译文,原文以细致的分析+大量的可视化图形演示了SVD的几何意义.能在有限的篇幅把 这个问题讲解的如此清晰,实属不易.原文举了一个简单的图像处理问题,简单形象, ...
- perf的采样模式和统计模式
perf的采样模式和统计模式 统计模式和采样模式使用寄存器的方法不相同; 在统计模式下,每次调度之前设置寄存器,调度之后清理寄存器,留个下个进程使用;PMU寄存器的使用方法; 在采样模式下,每次 pm ...
- 有道云笔记web版本居然不支持火狐
好尴尬的火狐呀....
- 第157天:canvas基础知识详解
目录 一.canvas简介 1.1 什么是canvas?(了解) 1.2 canvas主要应用的领域(了解) 二.canvas绘图基础 2.0 sublime配置canvas插件(推荐) 2.1 Ca ...
- 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中
一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字. 二.利用代码给ListView添加 ...
- HDU4753 Fishhead’s Little Game——2013 ACM/ICPC Asia Regional Nanjing Online
今天比赛又是做得好水的.被狂虐啊. 比赛两个多小时一直没出题,遒遒最先交的若干发都wa了.T_T 我独自在一遍苦思了1006这个题,还好最后把这个题目A掉了,不然又是深坑队友. 题目的意思我就不多说了 ...
- Day21-自定义simple_tag/filter
一. 在模板里面对用户发过来的数据进行二次加工.想办法把Python的函数放到模板语言里面使用. 有2种方法:filter与simple_tag 二. 实例 {{name|lower}} trunca ...