fuzhi的真相 2

	Person a1=new Person();
		Person b1=new Person();
		a1.age=32;
		b1.age=25;
		print("first: age of a1:"+a1.age+"; age of b1"+b1.age);
		a1.changeAge(b1);
		print("first: age of a1:"+a1.age+"; age of b1"+b1.age);
		a1.changeAge2(b1.clone());
		print("first: age of a1:"+a1.age+"; age of b1"+b1.age);

b1使用clone方法新构建一个对象赋值给changeage2的方法 这样就不会修改b1本身的成员

当然在person类中也要重写父类的clone方法
public Person clone()
{
	Object person=null;
	try
	{
		person=super.clone();
		return (Person)person;
	} catch (Exception e)
	{
		 return null;
	}
}
}


first: age of a:32; age of b25(2)
first: age of a:25; age of b25(2)
first: age of a:67; age of b67(2)
first: age of a:31; age of b31(2)
first: age of a1:32; age of b125(2)
first: age of a1:32; age of b143(2)
first: age of a1:32; age of b143(2)

你可能感兴趣的:(fuzhi的真相 2)