-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
59 lines (48 loc) · 1.35 KB
/
Swap.java
File metadata and controls
59 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.base;
public class Swap {
public static void main(String[] args) {
int x = 5;
int y = 10;
swap(x,y);
System.out.println("使用swap1()交换:");
System.out.println(x);
System.out.println(y);
Value v = new Value(5,10);
swap(v);
System.out.println("使用swap()2交换:");
System.out.println(v.getX());
System.out.println(v.getY());
}
// 无效的交换:形参的改变无法反作用于实参
public static void swap(int x,int y) {
int temp = x;
x = y;
y = temp;
}
// 有效的交换:通过引用(变量指向一个对象)来修改成员变量
public static void swap(Value value) {
int temp = value.getX();
value.setX(value.getY() );
value.setY(temp);
}
}
class Value{
private int x;
private int y;
public Value(int x,int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}