详解Java中的指针、引用及对象的clone(4)
2008-02-23 09:32:40来源:互联网 阅读 ()
package clone;
class CloneC implements Cloneable{
public String str;
public StringBuffer strBuff;
public Object clone(){
CloneC o = null;
try{
o = (CloneC)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
public class StrClone {
public static void main(String[] a)
{
CloneC c1 = new CloneC();
c1.str = new String("initializeStr");
c1.strBuff = new StringBuffer
("initializeStrBuff");
System.out.println("before
clone,c1.str = " c1.str);
System.out.println("before
clone,c1.strBuff = " c1.strBuff);
c2.str = c2.str.substring(0,5);
c2.strBuff = c2.strBuff.append
(" change strBuff clone");
System.out.println("=================================");
System.out.println("after
clone,c1.str = " c1.str);
System.out.println
("after clone,c1.strBuff = " c1.strBuff);
System.out.println("=================================");
System.out.println("after
clone,c2.str = " c2.str);
System.out.println
("after clone,c2.strBuff = " c2.strBuff);
}
}
/* RUN RESULT
before clone,c1.str = initializeStr
before clone,c1.strBuff = initializeStrBuff
=================================
after clone,c1.str = initializeStr
after clone,c1.strBuff =
initializeStrBuff change strBuff clone
=================================
after clone,c2.str = initi
after clone,c2.strBuff =
initializeStrBuff change strBuff clone
*
*/
package clone; public class StrTest
{
public static void main(String[] args)
{
String str1 = "This is a test for immutable";
String str2 = str1.substring(0,8);
System.out.println("print str1 : " str1);
System.out.println("print str2 : " str2);
}
}
/* RUN RESULT print str1 :
This is a test for immutable print str2 :
This is */
c2.str = c2.str.substring(0,5);
c2.strBuff = c2.strBuff.append
(" change strBuff clone");
c2.str.substring(0,5);
c2.strBuff.append
(" change strBuff clone");
c2.str.substring(0,5);
上一篇: J2EE修炼之四书五经
下一篇: JAVA对象序列化保存为XML文件的工具类
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:Beanutils (1)
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash

