来了来了,完坑了
我们来看看这两个代码,
package com.cskaoyan.object.syntax.methodarg;
/**
* @version 1.0
*/
class Demo2 {
int a;
}
public class TestQuote2 {
public static void main(String args[]) {
Demo2 d1 = new Demo2();
d1.a = 1;
Demo2 d2 = new Demo2();
d2.a = 2;
System.out.println(d1.a); //1
System.out.println(d2.a);// 2
function(d1, d2);
System.out.println(d1.a);
System.out.println(d2.a);
}
private static void function(Demo2 d1, Demo2 d2) {
Demo2 temp;
temp = d1;
d1 = d2;
d2 = temp;
//System.out.println("function: " + "d1.a = " + d1.a + ", d2.a = " + d2.a);
}
}
package com.cskaoyan.object.syntax.methodarg;
/**
* @version 1.0
*/
class Demo1 {
int a;
}
public class TestQuote1 {
public static void main(String args[]) {
Demo1 d1 = new Demo1();
d1.a = 1;
Demo1 d2 = new Demo1();
d2.a = 2;
System.out.println(d1.a);
System.out.println(d2.a);
function(d1, d2);
System.out.println(d1.a);
System.out.println(d2.a);
}
// 方法的参数类型,都是引用类型
private static void function(Demo1 d1, Demo1 d2) {
//交换 d1 和 d2这两个引用变量所指向的对象的 成员变量a的值
int tmp; //局部变量
tmp = d1.a;
d1.a = d2.a;
d2.a = tmp;
}
}
你猜出来这两个的正确输出值吗
我们来看看在内存的存储情况
行了。