From feb54b50d93e5a02a64e9176b07fdde2555b078c Mon Sep 17 00:00:00 2001 From: GrumpyCitizenBear <1049378767@qq.com> Date: Thu, 7 Oct 2021 16:39:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8C=89=E7=85=A7=E5=A4=9A=E9=87=8D=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/github/hcsp/polymorphism/Point.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/Point.java b/src/main/java/com/github/hcsp/polymorphism/Point.java index 780e6b1..25448df 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Point.java +++ b/src/main/java/com/github/hcsp/polymorphism/Point.java @@ -1,13 +1,17 @@ package com.github.hcsp.polymorphism; +import 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 { private final int x; private final int y; + // 代表笛卡尔坐标系中的一个点 public Point(int x, int y) { this.x = x; @@ -53,7 +57,11 @@ public String toString() { // 按照先x再y,从小到大的顺序排序 // 例如排序后的结果应该是 (-1, 1) (1, -1) (2, -1) (2, 0) (2, 1) - public static List sort(List points) {} + public static List sort(List points) { + Collections.sort(points); + return points; + + } public static void main(String[] args) throws IOException { List points = @@ -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; + } }