登录  /  注册
首页 > Java > java教程 > 正文

如何在Java中交换对象数据?(代码实例)

藏色散人
发布: 2019-03-19 11:52:48
原创
4599人浏览过

假设我们有一个名为“car”的类,它具有一些属性。我们创建了car的两个对象,car1和car2,如何交换car1和car2的数据?

如何在Java中交换对象数据?(代码实例)

一个简单的解决方案是交换成员。例如,如果类Car只有一个integer(整数)属性“no”(Car number),我们可以通过简单地交换两辆车的成员来交换汽车。

class Car 
{ 
    int no; 
    Car(int no) { this.no = no; } 
} 
  
class Main 
{  
    public static void swap(Car c1, Car c2) 
    { 
        int temp = c1.no; 
        c1.no = c2.no; 
        c2.no = temp; 
    } 
    
    public static void main(String[] args) 
    { 
        Car c1 = new Car(1); 
        Car c2 = new Car(2); 
        swap(c1, c2); 
        System.out.println("c1.no = " + c1.no); 
        System.out.println("c2.no = " + c2.no); 
    } 
}
登录后复制

输出:

c1.no = 2
c2.no = 1
登录后复制

如果我们不知道Car的成员呢?

上面的解决方案是有效的,因为我们知道Car中有一个成员“no”。如果我们不知道Car的成员或者成员列表太大怎么办。这是一种非常常见的情况,因为使用其他类的类可能无法访问其他类的成员。下面的解决方案有效吗?

class Car 
{ 
    int model, no; 
  
    Car(int model, int no) 
    { 
        this.model = model; 
        this.no = no; 
    } 
  
    void print() 
    { 
        System.out.println("no = " + no + 
                           ", model = " + model); 
    } 
} 
  
class Main 
{ 
    public static void swap(Car c1, Car c2) 
    { 
        Car temp = c1; 
        c1 = c2; 
        c2 = temp; 
    } 
  
    public static void main(String[] args) 
    { 
        Car c1 = new Car(101, 1); 
        Car c2 = new Car(202, 2); 
        swap(c1, c2); 
        c1.print(); 
        c2.print(); 
    } 
}
登录后复制

输出:

no = 1, model = 101
no = 2, model = 202
登录后复制

从上面的输出中我们可以看到,没有交换对象。参数在Java中是通过值传递的。因此,当我们将c1和c2传递给swap()时,swap()函数会创建这些引用的副本。

解决方案是使用Wrapper类如果我们创建一个包含Car引用的包装类,我们可以通过交换Wrapper类的引用来交换Car。

class Car 
{ 
    int model, no; 
  
    Car(int model, int no) 
    { 
        this.model = model; 
        this.no = no; 
    } 
  
    void print() 
    { 
        System.out.println("no = " + no +  
                           ", model = " + model); 
    } 
} 
  
class CarWrapper 
{ 
   Car c; 
  
   CarWrapper(Car c)   {this.c = c;} 
} 
  
class Main 
{ 
    public static void swap(CarWrapper cw1,  
                            CarWrapper cw2) 
    { 
        Car temp = cw1.c; 
        cw1.c = cw2.c; 
        cw2.c = temp; 
    } 
  
    public static void main(String[] args) 
    { 
        Car c1 = new Car(101, 1); 
        Car c2 = new Car(202, 2); 
        CarWrapper cw1 = new CarWrapper(c1); 
        CarWrapper cw2 = new CarWrapper(c2); 
        swap(cw1, cw2); 
        cw1.c.print(); 
        cw2.c.print(); 
    } 
}
登录后复制

输出:

no = 2, model = 202
no = 1, model = 101
登录后复制

因此,即使user 类无法访问要交换对象的类的成员,Wrapper类解决方案仍然有效。

相关推荐:《Java教程

本篇文章就是关于Java交换对象的方法介绍,希望对需要的朋友有所帮助!

以上就是如何在Java中交换对象数据?(代码实例)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号