为了理解如何在爪哇中交换对象,我们考虑下面的说明 跟随 :
插图:
假设我们有一个名为“Car”的类,它有一些属性。我们创建了两个Car对象,比如car1和car2,如何交换car1和car2的数据?
方法:
- 使用面向对象的概念
- 使用java的包装类
方法1: 使用面向对象的概念
在这里,我们将简单地交换成员 L 让我们直接拿一个我们将使用的“汽车”示例。因此,如果类“Car”只有一个整数属性,比如“no”(Car number),我们可以通过简单地交换两个Car的成员来交换Car。
例1-A
JAVA
// Java program to demonstrate that we can swap two // objects be swapping members // Class 1 // Number class Car class Car { // Attributes associated with car int no; Car( int no) { this .no = no; } } // Class 2 // Uses Car objects class GFG { // Method 1 // To swap public static void swap(Car c1, Car c2) { int temp = c1.no; c1.no = c2.no; c2.no = temp; } // Method 2 // Main driver method public static void main(String[] args) { // Creating car class objects(creating cars) Car c1 = new Car( 1 ); Car c2 = new Car( 2 ); // Calling method 1 swap(c1, c2); // Print and display commands System.out.println( "c1.no = " + c1.no); System.out.println( "c2.no = " + c2.no); } } |
c1.no = 2c2.no = 1
注: 极客,如果我们不认识车里的人呢?
上述解决方案起到了作用,因为我们知道车里有一个成员“no”。如果我们不认识Car的成员或者成员名单太大怎么办。T his是一种非常常见的情况,因为使用其他类的类可能无法访问其他类的成员 .下面的解决方案有效吗?
例1-B
JAVA
// Java program to demonstrate that we can swap two // objects be swapping members // Where it does not work // Class 1 // A car with number and name class Car { // Attributes of Car class int model, no; // Constructor Car( int model, int no) { // This keyword is used to refer // current instance itself this .model = model; this .no = no; } // Method of this class // To print Car void print() { // Printing number and model of car System.out.println( "no = " + no + ", model = " + model); } } // Class 2 // 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 = 101no = 2, model = 202
输出说明: 正如我们从上面的输出中所看到的,对象没有交换。我们已经讨论过了 以前的职位 在Java中,参数是通过值传递的。因此,当我们将c1和c2传递给swap()时,swap()函数会创建这些引用的一个副本。
方法2: 包装类
如果我们创建一个包含Car引用的包装器类,我们可以通过交换包装器类的引用来交换Car。
实例
JAVA
// Java program to Demonstrate that Wrapper Classes // Can be Used to Swap two Objects // Class 1 // A car with model and no. class Car { // Attributes associated with car int model, no; // Constructor of class 1 Car( int model, int no) { // This refers to current instance itself this .model = model; this .no = no; } // Method // To print object details void print() { System.out.println( "no = " + no + ", model = " + model); } } // Class 2 // Wrapper over class that is used for swapping class CarWrapper { Car c; // Constructor CarWrapper(Car c) { this .c = c; } } // Class 3 // Uses Car class and swaps objects of Car // using CarWrapper class GFG { // 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; } // Main 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
因此,即使用户类无权访问要交换其对象的类的成员,包装类解决方案仍然有效。 本文由 阿努拉格·雷 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。