对象 互换 java_如何在Java中交换对象? - Break易站

Java 类和对象

如何在Java中交换对象?

比方说,我们有一个名为“汽车”的类有一些属性。我们创造两个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 Car

class Car

{

int no;

Car(int no) { this.no = no; }

}

// A class that uses Car objects

class 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 = 2

c2.no = 1

如果我们不认识Car的成员,该怎么办?

上述解决方案的工作原理是,我们知道Car中有一个成员“no”。如果我们不认识Car的会员或会员名单太大,该怎么办?Ť 他是一个很常见的情况是使用一些其他的类不能访问其他类的成员类。下面的解决方案工作?

// A Java program to demonstrate that simply swapping

// object references doesn't work

// A car with number and name

class 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 Car

class 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 = 101

no = 2,model = 202

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

解决方案是使用Wrapper类

如果我们创建一个包含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 swapping

class CarWrapper

{

Car c;

// Constructor

CarWrapper(Car c) {this.c = c;}

}

// A Class that use Car and swaps objects of Car

// using CarWrapper

class 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 = 202

no = 1,model = 101

因此,即使用户类无法访问要交换对象的类的成员,包装类解决方案也能正常工作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值