对象复制例子
所有类对象都只引用同一个类,通过构造函数Test(final Test oldtest)可以复制出一个新的对象 import java.util.*; public class Test { String myname; String mysexy; Test(String name,String Sexy) { myname = name; mysexy = Sexy; } Test(final Test oldtest)//构造函数复制一个新类 { myname = oldtest.myname; mysexy = oldtest.mysexy; } public void setname(String setname) { myname = setname; } public void setsexy(String setsexy) { mysexy = setsexy; } public static void main(String args[]) { Test test1 = new Test("jack","male"); Test test2 = new Test(test1); test2.setname("peter"); test2.setsexy("female"); System.out.print("test1:"+test1.myname+",sexy is:"+test1.mysexy); System.out.print("/ntest2:"+test2.myname+",sexy is:"+test2.mysexy); } }