欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中的克隆方法的使用(三类 浅、深、完美克隆 ) 博客分类: java java克隆 

程序员文章站 2024-03-26 12:12:17
...
package clone;                           //本例子阐释了clone的魅力,影子克隆
class UnCloneA { 
	private int i; 
	public UnCloneA(int ii) {
		i = ii; 
		} 
	public void doublevalue() {
		i *= 2; 
		} 
	public String toString() { 
		return Integer.toString(i);      //把整数转换为字符串
		} 
	} 

class CloneB implements Cloneable{     //实现了克隆接口 Cloneable
	public int aInt;
	public UnCloneAA unCA = new UnCloneAA(111);
	
	public Object clone(){                         //重写clone方法
		CloneB o = null; 
		try{
			o = (CloneB)super.clone(); 
			}catch(CloneNotSupportedException e){
				e.printStackTrace(); 
				} 
		return o; 
		}
	} 
public class CloneMain影子克隆 {
	public static void main(String[] a){
		CloneB b1 = new CloneB(); 
		b1.aInt = 11; 
		System.out.println("before clone,b1.aInt = "+ b1.aInt); 
		System.out.println("before clone,b1.unCA = "+ b1.unCA); 
		CloneB b2 = (CloneB)b1.clone();                         //b1的克隆
		b2.aInt = 22;           
		b2.unCA.doublevalue(); 
		System.out.println("================================="); 
		System.out.println("after clone,b1.aInt = "+ b1.aInt); 
		System.out.println("after clone,b1.unCA = "+ b1.unCA);      //已经被改变了
		System.out.println("================================="); 
		System.out.println("after clone,b2.aInt = "+ b2.aInt); 
		System.out.println("after clone,b2.unCA = "+ b2.unCA); 
	}
}

/** RUN RESULT:
 *  before clone,b1.aInt = 11 before clone,b1.unCA = 111 
 *  ================================= 
 *  after clone,b1.aInt = 11 after clone,b1.unCA = 222 
 * ================================= 
 * after clone,b2.aInt = 22 after clone,b2.unCA = 222 */
 

package clone;

class UnCloneAA implements Cloneable{ 
	private int i; 
	public UnCloneAA(int ii) {
		i = ii; 
		} 
	public void doublevalue() { 
		i *= 2; 
		} 
	public String toString() {
		return Integer.toString(i);
		} 
	public Object clone(){                        //深度克隆这个类也要重写这个方法
		UnCloneAA o = null;
		try{ 
			o = (UnCloneAA)super.clone();
			}catch(CloneNotSupportedException e){
				e.printStackTrace(); 
				} 
		return o; 
		} 
	} 
class CloneBB implements Cloneable{ 
	public int aInt; 
	public UnCloneAA unCAA = new UnCloneAA(111); 
	public Object clone(){                         
		CloneBB o = null; 
	try{ 
		o = (CloneBB)super.clone(); 
		}catch(CloneNotSupportedException e){ 
			e.printStackTrace(); 
			} 
	o.unCAA = (UnCloneAA)unCAA.clone();              //深度克隆必加的一句
	return o; 
	} 
	} 
public class CloneMain深度克隆 {
	public static void main(String[] a){ 
		CloneBB b1 = new CloneBB(); 
		b1.aInt = 11; 
		System.out.println("before clone,b1.aInt = "+ b1.aInt); 
		System.out.println("before clone,b1.unCA = "+ b1.unCAA); 
		CloneBB b2 = (CloneBB)b1.clone();                             //克隆
		b2.aInt = 22;
		b2.unCAA.doublevalue(); 
		System.out.println("================================="); 
		System.out.println("after clone,b1.aInt = "+ b1.aInt); 
		System.out.println("after clone,b1.unCA = "+ b1.unCAA); 
		System.out.println("================================="); 
		System.out.println("after clone,b2.aInt = "+ b2.aInt); 
		System.out.println("after clone,b2.unCA = "+ b2.unCAA); 
}
	}
/*before clone,b1.aInt = 11
before clone,b1.unCA = 111
=================================
after clone,b1.aInt = 11
after clone,b1.unCA = 111
=================================
after clone,b2.aInt = 22
after clone,b2.unCA = 222
*/


package clone;

class UnCloneAAA implements Cloneable{ 
	private int i; 
	public UnCloneAAA(int ii) { 
		i = ii; 
		} 
	public void doublevalue() { 
		i *= 2; 
		} 
	public String toString() { 
		return Integer.toString(i);
		} 
	public Object clone(){ 
		UnCloneAAA o = null;
		try{ 
			o = (UnCloneAAA)super.clone();
		}catch(CloneNotSupportedException e){ 
			e.printStackTrace(); 
			} 
		return o; 
		} 
	} 
class CloneBBB implements Cloneable{ 
	public int aInt; 
	public UnCloneAAA unCAAA = new UnCloneAAA(111); 
	public Object clone(){
		CloneBBB o = null; 
		try{
			o = (CloneBBB)super.clone();
			}catch(CloneNotSupportedException e){ 
				e.printStackTrace(); 
				} 
		o.unCAAA = (UnCloneAAA)unCAAA.clone(); 
		return o; 
		} 
	} 
public class CloneMain完美克隆 { 
	public static void main(String[] a){ 
		CloneBBB b1 = new CloneBBB(); 
		b1.aInt = 11; 
		System.out.println("before clone,b1.aInt = "+ b1.aInt);
		System.out.println("before clone,b1.unCA = "+ b1.unCAAA); 
		CloneBBB b2 = (CloneBBB)b1.clone();
		b2.aInt = 22; 
		b2.unCAAA.doublevalue(); 
		System.out.println("================================="); 
		System.out.println("after clone,b1.aInt = "+ b1.aInt); 
		System.out.println("after clone,b1.unCA = "+ b1.unCAAA); 
		System.out.println("================================="); 
		System.out.println("after clone,b2.aInt = "+ b2.aInt); 
		System.out.println("after clone,b2.unCA = "+ b2.unCAAA); 
	} 
}
/*before clone,b1.aInt = 11
before clone,b1.unCA = 111
=================================
after clone,b1.aInt = 11
after clone,b1.unCA = 111
=================================
after clone,b2.aInt = 22
after clone,b2.unCA = 222
*/






相关标签: java 克隆