Skip to content

Commit d3e25f5

Browse files
完善程序。
1 parent 192e4ad commit d3e25f5

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/base/Swap.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public static void main(String[] args) {
1212
Value v = new Value(5,10);
1313
swap(v);
1414
System.out.println("使用swap()2交换:");
15-
System.out.println(v.x);
16-
System.out.println(v.y);
15+
System.out.println(v.getX());
16+
System.out.println(v.getY());
1717
}
1818

1919
// 无效的交换:形参的改变无法反作用于实参
@@ -25,19 +25,35 @@ public static void swap(int x,int y) {
2525

2626
// 有效的交换:通过引用(变量指向一个对象)来修改成员变量
2727
public static void swap(Value value) {
28-
int temp = value.x;
29-
value.x = value.y;
30-
value.y = temp;
28+
int temp = value.getX();
29+
value.setX(value.getY() );
30+
value.setY(temp);
3131
}
3232
}
3333

3434
class Value{
35-
int x;
36-
int y;
35+
private int x;
36+
private int y;
3737

3838
public Value(int x,int y) {
3939
this.x = x;
4040
this.y = y;
4141
}
42+
43+
public int getX() {
44+
return x;
45+
}
46+
47+
public void setX(int x) {
48+
this.x = x;
49+
}
50+
51+
public int getY() {
52+
return y;
53+
}
54+
55+
public void setY(int y) {
56+
this.y = y;
57+
}
4258
}
4359

0 commit comments

Comments
 (0)