当前位置: 技术问答>java相关
Cappuccino : The Proper Clone
来源: 互联网 发布时间:2017-03-27
本文导语: Cappuccino : The Proper Clone 1. Cloning is the process of creating a copy of an object. 2. Java provides a mechanism for objects to create copies of themselves by using the java.lang.Cloneable interface and the Object.clone() method. 3. The Object.cl...
Cappuccino : The Proper Clone
1. Cloning is the process of creating a copy of an object.
2. Java provides a mechanism for objects to create copies of themselves by using the java.lang.Cloneable interface and the Object.clone() method.
3. The Object.clone() method is protected, so only subclasses of Object and classes in the same package can access the method.
4. The Object.clone() method is declared as native. Because it is native, its implementation lies within the JVM itself.
5. If your object tries to call the Object.clone() method without implementing the Cloneable interface a CloneNotSupportedException will be thrown.
6. A shallow copy of an object is a copy in which only the primitive and reference values are copied to the new object. This means that object members like ints, floats, and booleans have the same values in both the new and existing objects, but object members like arrays, Hashtables, Vectors, and so on are shared between the new and existing objects.
7. A deep copy of an object differs from a shallow copy because all nonprimitive object members are cloned as well. In a deep copy, if an existing object has a Vector object as a member, a clone operation on the existing object results in a new object being created that points to a new Vector object as well.
8. In addition to shallow and deep copies, a mixture of the two can be used. One such mixture, which I will call a mutable deep copy, would copy only those objects that can be changed (mutable), but it would not copy objects that cannot be changed (immutable).
9. It is important to realize that you do not need to create copies of immutable objects when cloning. Because the object values themselves cannot change you would only be wasting memory. It is important, though, to make copies of mutable objects to prevent different copies of an object from sharing references to the same mutable object.
10. JurassicPark.java Source Codes
package org.jfml.cappuccino;
import java.util.*;
public class JurassicPark
{
public static void main(String[] args)
{
Dinosaur d1 = new Dinosaur("d1", "Patrick", "Vegetarian", 120.4);
Dinosaur d2 = (Dinosaur)d1.clone();
d2.setName("d2");
d2.setCloner("Tony");
d2.setType("Carnivoe");
d2.setWeight(451.3);
System.out.println(d1.toString());
System.out.println("n");
System.out.println(d2.toString());
}
}
class Dinosaur
implements Cloneable
{
private String name;
private Scientist cloner = new Scientist();
private Hashtable attributes = new Hashtable();
public Dinosaur(String name, String clonerName, String type, double weight)
{
this.setName(name);
this.setCloner(clonerName);
this.setType(type);
this.setWeight(weight);
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setCloner(String clonerName)
{
this.cloner.name = clonerName;
}
public Scientist getCloner()
{
return this.cloner;
}
public void setType(String type)
{
this.attributes.put("Type", type);
}
public String getType()
{
return (String)this.attributes.get("Type");
}
public void setWeight(double weight)
{
this.attributes.put("Weight", new Double(weight));
}
public double getWeight()
{
return ((Double)this.attributes.get("Weight")).doubleValue();
}
public Object clone()
{
Dinosaur dns = null;
try
{
dns = (Dinosaur)super.clone();
dns.cloner = (Scientist)this.getCloner().clone();
dns.attributes = (Hashtable)this.attributes.clone();
}
catch (Exception e)
{
e.printStackTrace();
}
if (null == dns)
{
throw new RuntimeException(this.getClass().getName() + ".clone() failed.");
}
return dns;
}
public String toString()
{
return
(
"Name: " + this.name + "n" +
"Cloner: " + this.cloner.name + "n" +
"Type: " + this.getType() + "n" +
"Weight: " + this.getWeight()
);
}
}
class Scientist
implements Cloneable
{
public String name;
public Object clone()
{
Scientist st = null;
try
{
st = (Scientist)super.clone();
}
catch (Exception e)
{
e.printStackTrace();
}
if (null == st)
{
throw new RuntimeException(this.getClass().getName() + ".clone() failed.");
}
return st;
}
}
1. Cloning is the process of creating a copy of an object.
2. Java provides a mechanism for objects to create copies of themselves by using the java.lang.Cloneable interface and the Object.clone() method.
3. The Object.clone() method is protected, so only subclasses of Object and classes in the same package can access the method.
4. The Object.clone() method is declared as native. Because it is native, its implementation lies within the JVM itself.
5. If your object tries to call the Object.clone() method without implementing the Cloneable interface a CloneNotSupportedException will be thrown.
6. A shallow copy of an object is a copy in which only the primitive and reference values are copied to the new object. This means that object members like ints, floats, and booleans have the same values in both the new and existing objects, but object members like arrays, Hashtables, Vectors, and so on are shared between the new and existing objects.
7. A deep copy of an object differs from a shallow copy because all nonprimitive object members are cloned as well. In a deep copy, if an existing object has a Vector object as a member, a clone operation on the existing object results in a new object being created that points to a new Vector object as well.
8. In addition to shallow and deep copies, a mixture of the two can be used. One such mixture, which I will call a mutable deep copy, would copy only those objects that can be changed (mutable), but it would not copy objects that cannot be changed (immutable).
9. It is important to realize that you do not need to create copies of immutable objects when cloning. Because the object values themselves cannot change you would only be wasting memory. It is important, though, to make copies of mutable objects to prevent different copies of an object from sharing references to the same mutable object.
10. JurassicPark.java Source Codes
package org.jfml.cappuccino;
import java.util.*;
public class JurassicPark
{
public static void main(String[] args)
{
Dinosaur d1 = new Dinosaur("d1", "Patrick", "Vegetarian", 120.4);
Dinosaur d2 = (Dinosaur)d1.clone();
d2.setName("d2");
d2.setCloner("Tony");
d2.setType("Carnivoe");
d2.setWeight(451.3);
System.out.println(d1.toString());
System.out.println("n");
System.out.println(d2.toString());
}
}
class Dinosaur
implements Cloneable
{
private String name;
private Scientist cloner = new Scientist();
private Hashtable attributes = new Hashtable();
public Dinosaur(String name, String clonerName, String type, double weight)
{
this.setName(name);
this.setCloner(clonerName);
this.setType(type);
this.setWeight(weight);
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setCloner(String clonerName)
{
this.cloner.name = clonerName;
}
public Scientist getCloner()
{
return this.cloner;
}
public void setType(String type)
{
this.attributes.put("Type", type);
}
public String getType()
{
return (String)this.attributes.get("Type");
}
public void setWeight(double weight)
{
this.attributes.put("Weight", new Double(weight));
}
public double getWeight()
{
return ((Double)this.attributes.get("Weight")).doubleValue();
}
public Object clone()
{
Dinosaur dns = null;
try
{
dns = (Dinosaur)super.clone();
dns.cloner = (Scientist)this.getCloner().clone();
dns.attributes = (Hashtable)this.attributes.clone();
}
catch (Exception e)
{
e.printStackTrace();
}
if (null == dns)
{
throw new RuntimeException(this.getClass().getName() + ".clone() failed.");
}
return dns;
}
public String toString()
{
return
(
"Name: " + this.name + "n" +
"Cloner: " + this.cloner.name + "n" +
"Type: " + this.getType() + "n" +
"Weight: " + this.getWeight()
);
}
}
class Scientist
implements Cloneable
{
public String name;
public Object clone()
{
Scientist st = null;
try
{
st = (Scientist)super.clone();
}
catch (Exception e)
{
e.printStackTrace();
}
if (null == st)
{
throw new RuntimeException(this.getClass().getName() + ".clone() failed.");
}
return st;
}
}
|
收藏
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。