}
public class Demo {
public static void main(String[] args) { //-------建立并构造两个对象--------- Person refa = new Person("张三", "男"); Person refb = new Person("李四", "男");
//交换refa对象和refb对象 Person.changeref(refa, refb); //从交换结果中看出,实际对象并未交换 System.out.println("在主函数中交换的结果 refa = " + refa.toString()); System.out.println("在主函数中交换的结果 refb = " + refb.toString()); //-------建立两个对象数组---------- Person[] arraya = new Person[1]; Person[] arrayb = new Person[1]; //分别构造数组对象 arraya[0] = new Person("王五","男"); arrayb[0] = new Person("赵六","男"); /*数组对象为null时,不能设置其值,必须先构造它(即调用构造函数),再用其它方法设置其值 */ System.out.println(’\n’+"数组对象交换前的结果 arraya = " + arraya[0].toString()); System.out.println("数组对象交换前的结果 arrayb = " + arrayb[0].toString()); //交换这两个数组对象 Person.changeArrayRef(arraya, arrayb); System.out.println("-交换后的结果 arraya = " + arraya[0].toString()); System.out.println("-交换后的结果 arrayb = " + arrayb[0].toString());
//-------建立两个普通数组--------- int[] a = new int[2]; int[] b = new int[2]; |