clearcase/" target="_blank" >cc6600" size="3">详解Java中的指针、引用及对象的clone 作者:中国DotNet俱乐部 来源:中国DotNet俱乐部
package reference;
class Obj
{
String str = "init value";
public String toString()
{
return str;
}
}
public class ObjRef
{
Obj aObj = new Obj();
int aInt = 11;
public void changeObj(Obj inObj)
{
inObj.str = "changed value";
}
public void changePri(int inInt)
{
inInt = 22;
}
public static void
main(String[] args)
{
ObjRef oRef = new ObjRef();
changeObj() method:
" + oRef.aObj);
oRef.changeObj(oRef.aObj);
System.out.println
("After call changeObj()
method: " + oRef.aObj);
("==================Print Primtive=================");
System.out.println("Before call
changePri() method: " + oRef.aInt);
oRef.changePri(oRef.aInt);
System.out.println("After call
changePri() method: " + oRef.aInt);
}
Before call changeObj() method:
init value
After call changeObj() method:
changed value
==================Print Primtive=================
Before call changePri() method: 11
After call changePri() method: 11
*/
package reference;
class PassObj
{
String str = "init value";
}
public class ObjPassvalue
{
main(String[] args)
{
PassObj objA = new PassObj();
PassObj objB = objA;
System.out.println
("Print objB.str value:
" + objB.str);
}
}
/* RUN RESULT
Print objB.str value:
changed in objA
*/
package reference;
import java.util.*;
public class HashtableAdd{
public static void
main(String[] args){
Hashtable ht = new Hashtable();
StringBuffer sb =
new StringBuffer();
sb.append("abc,");
ht.put("1",sb);
sb.append("def,");
ht.put("2",sb);
sb.append("mno,");
ht.put("3",sb);
sb.append("xyz.");
ht.put("4",sb);
Enumeration it = ht.elements();
while(it.hasMoreElements())
{
System.out.print("get StringBufffer
"+(++numObj)+" from Hashtable: ");
System.out.println(it.nextElement());
}
}
}
get StringBufffer 1
from Hashtable: abc,
get StringBufffer 2
from Hashtable: abc,def,
get StringBufffer 3
from Hashtable: abc,def,mno,
get StringBufffer 4
from Hashtable: abc,def,mno,xyz.
/* RUN RESULT
get StringBufffer 1
from Hashtable: abc,def,mno,xyz.
get StringBufffer 2
from Hashtable: abc,def,mno,xyz.
get StringBufffer 3
from Hashtable: abc,def,mno,xyz.
get StringBufffer 4
from Hashtable: abc,def,mno,xyz.
*/
new String("Hello NDP")).substring(0,3)
//RETURN RESULT: Hel
class CloneClass
implements Cloneable
{
public int aInt;
public Object clone()
{
CloneClass o = null;
try{
o = (CloneClass)super.clone();
}catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
return o;
}
}
package 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{
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object 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);
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.ext;
class UnCloneA implements Cloneable{
private int i;
public UnCloneA(int ii) { i = ii; }
public void doublevalue() { i *= 2; }
public String toString() {
return Integer.toString(i);
}
public Object clone(){
UnCloneA o = null;
try{
o = (UnCloneA)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return o;
}
}
class CloneB implements Cloneable{
public int aInt;
public UnCloneA unCA = new UnCloneA(111);
public Object clone(){
CloneB o = null;
try{
o = (CloneB)super.clone();
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
o.unCA = (UnCloneA)unCA.clone();
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);
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);
}
}
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
*/
o.unCA = new StringBuffer
(unCA.toString());
//原来的是:
o.unCA = (UnCloneA)unCA.clone();
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);