java中实现两个数字的交换
package pro1;
class Num{
private int num;
public Num(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
public class M1 {
static void switch_(Num n1,Num n2){
int temp = n1.getNum();
n1.setNum(n2.getNum());
n2.setNum(temp);
}
public static void main(String[] args) {
Num n1 = new Num(10);
Num n2 = new Num(20);
System.out.println("Before swapping, n1 = " + n1.getNum() + " and n2 = " + n2.getNum());
switch_(n1, n2);
System.out.println("After swapping, n1 = " + n1.getNum() + " and n2 = " + n2.getNum());
}
}