Java传递参数对实体的影响
1、当传递的为变量时,例如int、String类型,改变不会影响实体的变化
package com.oop;
public class Test {
public static void main(String[] args) {
int t = 1;
A a = new A(t);
System.out.println(t);
}
}
class A {
int t;
public A (int t) {
this.t = t;
t = t + 1;
}
}
输出t的值为1
2、当传递参数为类时,改变将会影响实体的变化
package com.oop;
public class Test {
public static void main(String[] args) {
A a = new A();
a.t = 1;
B b = new B(a);
b.change();
System.out.println(a.t);
}
}
class A {
int t;
}
class B {
A a;
public B(A a) {
this.a = a;
}
public void change () {
this.a.t = 100;
}
}
输出a.t的值为100

浙公网安备 33010602011771号