java中引用类型传参不是传的是地址吗?那为什么执行完方法之后,值不发生改变?
为什么不选A啊?{:10_266:} 5.给定Person类的以下定义,class Person {
public String name;
public int height;
}
以下代码的输出是什么?B
class EJavaGuruPassObjects1 {
public static void main(String args[]) {
Person p = new Person();
p.name = "EJava";
anotherMethod(p);
System.out.println(p.name);
someMethod(p);
System.out.println(p.name);
}
static void someMethod(Person p) {
p.name = "someMethod";
System.out.println(p.name);
}
static void anotherMethod(Person p) {
p = new Person();
p.name = "anotherMethod";
System.out.println(p.name);
}
}
a anotherMethod
anotherMethod
someMethod
someMethod
b anotherMethod
EJava
someMethod
someMethod
c anotherMethod
EJava
someMethod
EJava
d 编译失败 java都是传值吧! static void anotherMethod(Person p) {
p = new Person();//这边重新赋值了,那就和main里面的p没关系了
p.name = "anotherMethod";
System.out.println(p.name);
} 逍遥YZ 发表于 2018-12-18 21:17
java都是传值吧!
谢谢啦~嘻嘻嘻 zlj19931010 发表于 2018-12-20 15:50
谢谢啦~{:10_297:} {:10_279:}
页:
[1]