Skip to content

Commit 3d72233

Browse files
authored
finish (#212)
finish (#212)
1 parent 2760d21 commit 3d72233

File tree

1 file changed

+24
-2
lines changed
  • src/main/java/com/github/hcsp/polymorphism

1 file changed

+24
-2
lines changed

src/main/java/com/github/hcsp/polymorphism/Point.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import java.io.IOException;
44
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Collections;
57
import java.util.List;
68

7-
public class Point {
9+
public class Point implements Comparable<Point> {
810

911
private final int x;
1012
private final int y;
13+
1114
// 代表笛卡尔坐标系中的一个点
1215
public Point(int x, int y) {
1316
this.x = x;
@@ -53,7 +56,10 @@ public String toString() {
5356

5457
// 按照先x再y,从小到大的顺序排序
5558
// 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1)
56-
public static List<Point> sort(List<Point> points) {}
59+
public static List<Point> sort(List<Point> points) {
60+
Collections.sort(points);
61+
return points;
62+
}
5763

5864
public static void main(String[] args) throws IOException {
5965
List<Point> points =
@@ -65,4 +71,20 @@ public static void main(String[] args) throws IOException {
6571
new Point(2, -1));
6672
System.out.println(Point.sort(points));
6773
}
74+
75+
@Override
76+
public int compareTo(Point that) {
77+
if (this.x < that.x) {
78+
return -1;
79+
} else if (this.x > that.x) {
80+
return 1;
81+
}
82+
83+
if (this.y < that.y) {
84+
return -1;
85+
} else if (this.y > that.y) {
86+
return 1;
87+
}
88+
return 0;
89+
}
6890
}

0 commit comments

Comments
 (0)