国产探花免费观看_亚洲丰满少妇自慰呻吟_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;	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连平县| 池州市| 大港区| 儋州市| 密山市| 双流县| 拉孜县| 淮阳县| 正阳县| 石渠县| 固始县| 宜昌市| 吉木萨尔县| 垣曲县| 济阳县| 廉江市| 龙泉市| 宁国市| 张家港市| 波密县| 仲巴县| 繁昌县| 清徐县| 海兴县| 汽车| 福建省| 宜兰市| 金阳县| 东城区| 安仁县| 广西| 凤阳县| 新闻| 开原市| 盱眙县| 凉山| 德阳市| 金寨县| 健康| 叙永县| 蒲城县|