国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 深拷貝 淺拷貝

2019-11-06 08:21:15
字體:
來源:轉載
供稿:網友

1、Object類有一個保護接口,但是繼承之后重寫也無法使用,必須繼承Cloneable接口,以下是關于深淺拷貝的測試代碼:

package com.busymonkey.test;public class son implements Cloneable {		PRivate String name;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}		public Object clone() {		Object out = null;		try {			out = super.clone();		} catch (CloneNotSupportedException e) {			e.printStackTrace();		}		return out;	}}

package com.busymonkey.test;public class Test implements Cloneable {		private String name;	private String text;	public son p = new son();	    public static void main(String[] args) {    	Test test1 = new Test("name1","111");    	test1.p.setName("wahaha");    	Test test2 = null;		test2 = (Test)test1.clone();    	test2.setName("name2");    	test2.setText("222");    	test2.p.setName("yeah");    	System.out.println(test1.getName()+" : "+test1.getText()+" : "+test1.p.getName());    	System.out.println(test2.getName()+" : "+test2.getText()+" : "+test2.p.getName());    }    public Object clone() {    	Test out = null;    	try {			out = (Test) super.clone();		} catch (CloneNotSupportedException e) {			e.printStackTrace();		}    	//out.p = (son)p.clone();//深拷貝    	return out;    }    	public Test(String name, String text) {		super();		this.name = name;		this.text = text;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public String getText() {		return text;	}	public void setText(String text) {		this.text = text;	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新昌县| 霞浦县| 香格里拉县| 花垣县| 寿阳县| 临朐县| 洛隆县| 定边县| 通州区| 锡林浩特市| 桦南县| 兴业县| 托里县| 澳门| 白朗县| 尤溪县| 光山县| 元阳县| 尼木县| 隆林| 和平区| 东莞市| 东辽县| 贡嘎县| 南宫市| 南丰县| 南宁市| 吉木萨尔县| 含山县| 莒南县| 邻水| 温宿县| 乌审旗| 乌海市| 汉沽区| 万全县| 石门县| 滕州市| 台中市| 阳信县| 临朐县|