Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/main/java/com/github/hcsp/polymorphism/Point.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.github.hcsp.polymorphism;

import javafx.print.Collation;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无用导入 - javafx.print.Collation 。


import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Point {
public class Point implements Comparable<Point> {

private final int x;
private final int y;

// 代表笛卡尔坐标系中的一个点
public Point(int x, int y) {
this.x = x;
Expand Down Expand Up @@ -53,7 +57,11 @@ public String toString() {

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

}

public static void main(String[] args) throws IOException {
List<Point> points =
Expand All @@ -65,4 +73,20 @@ public static void main(String[] args) throws IOException {
new Point(2, -1));
System.out.println(Point.sort(points));
}

@Override
public int compareTo(Point o) {
if (this.x < o.x) {
return -1;
} else if (this.x > o.x) {
return 1;
}

if (this.y < o.y) {
return -1;
} else if (this.y > o.y) {
return 1;
}
return 0;
}
}