交换两个变量的值
class MyValue {
private int val;
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return val;
}
}
public class Test {
public static void swap(MyValue value1,MyValue value2){
int temp = value1.getVal();
value1.setVal(value2.getVal());
value2.setVal(temp);
}
public static void main(String[] args) {
MyValue value1 = new MyValue();
value1.setVal(10);
MyValue value2 = new MyValue();
value2.setVal(30);
System.out.println("原来的:" + value1.getVal() + " " +value2.getVal());
swap(value1,value2);
System.out.println("交换后:" + value1.getVal() + " " +value2.getVal());
}
}