日期:2014-05-20  浏览次数:20700 次

如何用基本数据类型的包装类改写下面程序?
想交换两个基本数据类型数据,如  
  int a = 89;  
  int b = 43;  
  swap(a, b);  
  输出 a = 43, b = 89  
  请问该swap(int, int)该如何实现

------解决方案--------------------
自己写个Integer的封装类吧。。。
Java code

package cn.sky;

public class Integer {
    private int num;

    public Integer(int num) {
        this.num = num;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return num + "";
    }
}

class Test {

    public static void main(String[] args) {
        Integer a = new Integer(89);
        Integer b = new Integer(43);
        swap(a,b);
        System.out.println(a + "---" + b);
    }

    static void swap(Integer a, Integer b) {
        int temp = b.getNum();
        b.setNum(a.getNum());
        a.setNum(temp);
    }

}