`

TestRefrence java参数传值

阅读更多
package zhch.illq.testsomething;

public class TestRefrence {

	public void test(Inner inner) {
		System.out.println("1test1:" + inner.abc);
		inner.abc = "changer";
		// 改变对象是没用的
		inner = new Inner("def");
		System.out.println("1test2:" + inner.abc);
	}

	public void test2(Inner inner) {
		System.out.println("2test1:" + inner.abc);
		// 可以改变对象的属性
		inner.abc = "changer";
		inner = new Inner("def");
		System.out.println("2test2:" + inner.abc);
	}

	public static void main(String[] args) {
		TestRefrence tt = new TestRefrence();
		Inner inner = new Inner("abc");
		System.out.println("out:" + inner.abc);
		tt.test(inner);
		System.out.println("out:" + inner.abc);
		tt.test2(inner);
		System.out.println("out:" + inner.abc);
		System.out.println("==================");
	}
}

class Inner {
	public String abc;

	public Inner(String abc) {
		this.abc = abc;
	}
}

out:abc
1test1:abc
1test2:def
out:changer
2test1:changer
2test2:def
out:changer
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics