import java.lang.reflect.*; public class ClassMain { public static void main(String[] args) { Birthday birth = new Birthday(1950,1,1); Assert one = new Assert(); //Assert two = null; //one.setAssert(50000, 80, 0); /*try { two = (Assert)one.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } one.Test(two); GrandPa pa = null; pa = new GrandPa(birth, one); GrandPa three = null; try { three = (GrandPa)pa.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } pa.Test(three);*/ //FamilyDemo.familyShow(pa);
//pa = new Father(); //FamilyDemo.familyShow(pa);
try { Class c = Class.forName("Assert"); Constructor[] cons = c.getDeclaredConstructors(); Class[] params=cons[0].getParameterTypes(); Object[] paramValues=new Object[params.length]; paramValues[0] = new Double(888888); paramValues[1] = new Double(8888); paramValues[2] = new Integer(8); Object o=cons[0].newInstance(paramValues); //System.out.println(o.getClass().getName()); Method[] ms=c.getDeclaredMethods(); for(int i=0;i<ms.length;i++) { System.out.println(ms[i]); } Object[] obj = new Integer[]{1,2}; ms[0].invoke(o,obj); }catch(Exception e) { e.printStackTrace(); } } }
public class GrandPa implements Cloneable { Birthday birth; Assert as;
浅CLONE就是不成功的CLONE,它发生在人为的疏忽上。记得这个就行了。 举例子来说,有一个类A,一个类B。
A类中都是基本对象, B类中除了基本对象,还包括A对象。那么,A对象的clone方法就可以很容易的写出来。
而B对象的clone方法中,还要实现 copy.setA(a.clone());
这样的方法。才可以。实现完全的CLONE否则的话,B的 clone得到的新对象b'中 ,
b'.a 与 b.a 是指向一个地址的。 b'.setA(a')后,
也会对b中的a产生影响。罗嗦这么多,反正你记得了,不正确的clone方法,就是浅拷贝。
public class ClassMain {
public static void main(String[] args) {
Birthday birth = new Birthday(1950,1,1);
Assert one = new Assert();
//Assert two = null;
//one.setAssert(50000, 80, 0);
/*try {
two = (Assert)one.clone();
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
one.Test(two);
GrandPa pa = null;
pa = new GrandPa(birth, one);
GrandPa three = null;
try {
three = (GrandPa)pa.clone();
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
pa.Test(three);*/
//FamilyDemo.familyShow(pa);
//pa = new Father();
//FamilyDemo.familyShow(pa);
try {
Class c = Class.forName("Assert");
Constructor[] cons = c.getDeclaredConstructors();
Class[] params=cons[0].getParameterTypes();
Object[] paramValues=new Object[params.length];
paramValues[0] = new Double(888888);
paramValues[1] = new Double(8888);
paramValues[2] = new Integer(8);
Object o=cons[0].newInstance(paramValues);
//System.out.println(o.getClass().getName());
Method[] ms=c.getDeclaredMethods();
for(int i=0;i<ms.length;i++) {
System.out.println(ms[i]);
}
Object[] obj = new Integer[]{1,2};
ms[0].invoke(o,obj);
}catch(Exception e) {
e.printStackTrace();
}
}
}
Birthday birth;
Assert as;
GrandPa(Birthday birth, Assert as) {
this.birth = birth;
this.as = as;
}
void show() {
System.out.println("==============");
birth.getBirthday();
as.getAssert();
}
public Object clone() throws CloneNotSupportedException {
GrandPa o = null;
o = (GrandPa)super.clone();
o.birth = (Birthday)birth.clone();
o.as = (Assert)as.clone();
return o;
}
public void Test(GrandPa a) {
a.birth = new Birthday(1951,2,2);
a.as = new Assert();
this.show();
a.show();
}
}
浅CLONE就是不成功的CLONE,它发生在人为的疏忽上。记得这个就行了。 举例子来说,有一个类A,一个类B。
A类中都是基本对象, B类中除了基本对象,还包括A对象。那么,A对象的clone方法就可以很容易的写出来。
而B对象的clone方法中,还要实现 copy.setA(a.clone());
这样的方法。才可以。实现完全的CLONE否则的话,B的 clone得到的新对象b'中 ,
b'.a 与 b.a 是指向一个地址的。 b'.setA(a')后,
也会对b中的a产生影响。罗嗦这么多,反正你记得了,不正确的clone方法,就是浅拷贝。有道理
o = (GrandPa)super.clone();
o.birth = (Birthday)birth.clone();
o.as = (Assert)as.clone();
return o;
这是浅克隆把?