不要用封装类,改成
Integer a=new Ingeter(10)
Integer b=new Ingeter(20)
void exc(Integer a,Integer b){}

解决方案 »

  1.   

    试了一下, liuchunjp() 的方法不行啊!
    superfishmanweb(我也是千百个不愿意呀):用stream的方法传object---能不能解释清楚一些啊?3X。
      

  2.   

    可行的一种方案,还是感觉不怎么好。还有没有别的?import java.io.*;public class st
    {
    void exc(int[]a,int[]b){
    int c=a[0];
    a[0]=b[0];
    b[0]=c;
    }

    public static void main(String args[])
    {
    int []a=new int[1];
    int []b=new int[1];
    a[0]=1;b[0]=2;
    st s=new st();
    System.out.println("a="+a[0]+";b="+b[0]+"\n");
    s.exc(a,b);
    System.out.println("a="+a[0]+";b="+b[0]);
    }
    }
      

  3.   

    对于liuchunjp() 的方法我是这样做的,不知道是不是有问题import java.io.*;public class st
    {
    void exc(Integer a,Integer b){
    Integer c=a;
    a=b;
    b=c;
    } public static void main(String args[])
    {
    Integer a=new Integer(1);
    Integer b=new Integer(2);
    st s=new st();
    System.out.println("a="+a+";b="+b+"\n");
    s.exc(a,b);//(Integer)
    System.out.println("a="+a+";b="+b);
    }
    }
      

  4.   

    不对,void exc(Integer a,Integer b){
    Integer c=a;
    a=b;
    b=c;
    }
    这个方法还是换汤不换药,传进去的还是引用,因为是值传递,所以引用不会被更改,所以结果不变。我所指的是,改变传进去的对象的内容。
    如果只是单纯想交换两个对象的话,只要简单的交换一下就可以了,何必又通过过程调用呢?对于封装类来说,是不可以改变其中的对象内容的。