标签:href .net ack rri 通过 change 实例 created alt
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
标签:href .net ack rri 通过 change 实例 created alt
原文地址:http://www.cnblogs.com/ttylinux/p/6580970.html