java 值传递
package cn.test; class G{ int a; int b; public G(int a,int b) { this.a=a; this.b=b; } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } @Override public String toString() { return "G [a=" + a + ", b=" + b + "]"; } } public class TestXinCan { public static void main(String[] args) { G g1=new G(1,1); System.out.println(g1); change(g1); System.out.println(g1); } public static void change(G g) { G g2=new G(2,2); //① /*g.setA(2); //② g.setB(2);*/ g=g2; } }
第一种方式 g1值不变,第二种方式g1值变了。