引用传参换值

package com.fcy;
/**
 * 
 * @Description: 方法传参
 */
public class TestCon {
	public static void swap(MyTest a, MyTest b) {
		int tmp = a.getNum();
		a.setNum(b.getNum());
		b.setNum(tmp);
	}

	public static void main(String[] args) {
		MyTest a = new MyTest(3);
		MyTest b = new MyTest(5);
		TestCon.swap(a, b);
		System.out.println(a.getNum() + " " + b.getNum());
	}
}

class MyTest {
	int Num;
	
	public MyTest(int Num){
		this.Num = Num;
	}
		
	public int getNum() {
		return Num;
	}

	public void setNum(int tmp) {
		Num = tmp;
	}
}

你可能感兴趣的:(java)