Java代码中如何交换两个对象
来源:互联网 发布:中建七局网络教育 编辑:程序博客网 时间:2024/05/21 04:43
原文地址:How to swap or exchange objects in Java?
Java代码如何交换对象
假设我们有一个叫Car的类,这个类中也有一些属性。我们创建Car类的两个对象,一个叫car1,另一个叫car2,那如何交换car1与car2的数据呢?
交换成员:例如,如果这个Car类只有一个整型的车号,我们可以通过交换两个车的号码来交换两个车。
// A Java program to demonstrate that we can swap two// objects be swapping members.// A car with number class Carclass Car{ int no; Car(int no) { this.no = no; }}// A class that uses Car objectsclass Main{ // To swap c1 and c2 public static void swap(Car c1, Car c2) { int temp = c1.no; c1.no = c2.no; c2.no = temp; } // Driver method 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 = 2c2.no = 1
如果我们不知道类的成员呢?
以上方法只能是在我们直到车号这个成员的时候才能使用,如果我们不知道Car的成员或者说成员列表太多了,那咋办?这是一个十分常见的情况,一个类用到其他的类,但是又不能访问这个类的成员。看看下面这段代码能不能搞定?
// A Java program to demonstrate that simply swapping// object references doesn't work// A car with number and nameclass Car{ int model, no; // Constructor Car(int model, int no) { this.model = model; this.no = no; } // Utility method to print Car void print() { System.out.println("no = " + no + ", model = " + model); }}// A class that uses Carclass Main{ // swap() doesn't swap c1 and c2 public static void swap(Car c1, Car c2) { Car temp = c1; c1 = c2; c2 = temp; } // Driver method 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 = 101no = 2, model = 202
从上面的输出可以看出,对象没有交换成功。我们在前面一章讨论过在Java中参数的传递。所以当我们传给swap方法c1与c2的时候,这个方法就创建了这两个引用的副本。
用封装类(Wrapper Class)解决,如果我们建立一个封装类,这个类包含Car的引用,我们可以通过交换封装类的引用来交换这两个car。
// A Java program to demonstrate that we can use wrapper// classes to swap to objects// A car with model and no.class Car{ int model, no; // Constructor Car(int model, int no) { this.model = model; this.no = no; } // Utility method to print object details void print() { System.out.println("no = " + no + ", model = " + model); }}// A Wrapper over class that is used for swappingclass CarWrapper{ Car c; // Constructor CarWrapper(Car c) {this.c = c;}}// A Class that use Car and swaps objects of Car// using CarWrapperclass Main{ // This method swaps car objects in wrappers // cw1 and cw2 public static void swap(CarWrapper cw1, CarWrapper cw2) { Car temp = cw1.c; cw1.c = cw2.c; cw2.c = temp; } // Driver method 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 = 202no = 1, model = 101
所以如果用户类不能访问到被交换的类的成员的时候,封装类可以解决这个问题。
0 0
- Java代码中如何交换两个对象
- Java 如何交换两个变量
- java中如何实现具有交换两个整数值
- 关于Java交换两个对象的问题
- 关于Java交换两个对象的问题
- 关于Java交换两个对象的问题
- java中交换两个数据的方法
- java中交换两个变量的方法
- 如何在Java中实现将两个数相互交换的方法
- Java中如何不用中间变量来使两个变量交换值
- 学习体会:在JAVA中如何判断两个对象是否相等
- java中如何判定两个对象属于同一类
- java中如何判定两个对象属于同一类
- java中如何判定两个对象属于同一类
- 如何交换两个数字
- java怎么实现两个对象内容的交换
- 用函数交换两个引用指向的对象--java
- java的Exchanger 两个线程可以交换对象Demo
- Android WebView 的回退方法(goback) 遭遇重定向
- 花生壳内网穿透功能设置
- Python图像处理库PIL中快速傅里叶变换FFT的实现(一)
- 【Android】Android防止过快点击造成多次事件执行(防止按钮重复点击)
- (16)数组
- Java代码中如何交换两个对象
- struts2常用标签详解
- Linux网络性能分析命令
- vb+dx泡泡堂dome
- 10.2创建sde服务不能启动问题
- 点击按钮,重置或者清空textarea中的数据
- [HNOI2008]GT考试
- 使用Ksoap2-Andorid调用WebService操作步骤
- Servlet的问题