实现交换两个变量的值。要求:需要交换实参的值。

package homework;

public class Chenge{
    static class Mytest{
        public int num;
    }
    public static void swp(Mytest a, Mytest b){
        int tmp = a.num;
        a.num = b.num;
        b.num = tmp;
    }

    public static void main(String[] args) {
        Mytest mytest1 = new Mytest();
        Mytest mytest2 = new Mytest();
        mytest1.num = 10;
        mytest2.num = 20;
        System.out.println("mytest1.num="+mytest1.num+"mytest2.num="+mytest2.num);
        swp(mytest1 ,mytest2);
        System.out.println("mytest1.num="+mytest1.num+"mytest2.num="+mytest2.num);
    }
}

重点在于传入swp函数中的是一个类,并且改变成员数据的值!

发布了28 篇原创文章 · 获赞 3 · 访问量 744

猜你喜欢

转载自blog.csdn.net/XDtobaby/article/details/102806641