public class hh {public static void main (String args[]){A aa = new A();aa.num =5;hh t = new hh();System.out.println("11 aa="+aa + "num="+aa.num);t.test(aa);System.out.println("22 aa="+aa + "num="+aa.num);} void test(A a){A ab = new A();a = ab;System

来源:学生作业帮助网 编辑:作业帮 时间:2024/11/03 05:19:42

public class hh {public static void main (String args[]){A aa = new A();aa.num =5;hh t = new hh();System.out.println("11 aa="+aa + "num="+aa.num);t.test(aa);System.out.println("22 aa="+aa + "num="+aa.num);} void test(A a){A ab = new A();a = ab;System
public class hh {
public static void main (String args[]){
A aa = new A();
aa.num =5;
hh t = new hh();
System.out.println("11 aa="+aa + "num="+aa.num);
t.test(aa);
System.out.println("22 aa="+aa + "num="+aa.num);
}
void test(A a){
A ab = new A();
a = ab;
System.out.println("33 ab="+ab + "num="+ab.num);
}
}
class A{
int num;
}

public class hh {public static void main (String args[]){A aa = new A();aa.num =5;hh t = new hh();System.out.println("11 aa="+aa + "num="+aa.num);t.test(aa);System.out.println("22 aa="+aa + "num="+aa.num);} void test(A a){A ab = new A();a = ab;System
输出结果应该是这样的
11 aa=..num=5
33 ab=..num=0
22 aa=..num=5
首先你new了一个A,并生成实例aa,然后赋值num=5,
所以第一行输出5
然后调用它t.test(a)方法,在方法里面,new了一个A,生成实例ab,但此时ab的num并未赋值,所以输出默认值0
最后,虽然aa作为参数传递到test中进行了操作,但实际并未改变aa本身num的值,所以最后输出5.
关于java中的参数传递和方法返回值需要了解下底层相关知识.