面向对象和作用域知识点小练

Java 中引用类型变量做参数传递时 所涉及的问题:
地址传递问题(区别于 值传递)

class test{
    
    
		public int a = 10;
}

public class TestValObject {
    
    
	public static void another(test v1,int i) {
    
    
		i=0;
		v1.a=40; // 局部变量v1存储 test变量的地址,(10)->(40)
		test v0 = new test();
		v1 = v0; // v1 指向改变
		System.out.println(v1.a);
		System.out.println(i);
	}
	public static void main(String[] args) {
    
    
		test v1 = new test();
		int i=20;
		another(v1, i); // 传递地址
		System.out.println(v1.a);
		System.out.println(i);
	}
}

// 10 0 40 20

(默默的加一句,我被坑的好惨呀!)

猜你喜欢

转载自blog.csdn.net/qq_43341057/article/details/104851882