From 3c22d87aa5e3177a81b93ecf798c5f70aefd98ac Mon Sep 17 00:00:00 2001 From: sltap Date: Mon, 22 Aug 2022 08:14:33 -0400 Subject: [PATCH] 14.3 --- .../com/github/hcsp/polymorphism/Point.java | 25 +++++++++++++++++-- 1 file changed, 23 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..dbd5f0d 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Point.java +++ b/src/main/java/com/github/hcsp/polymorphism/Point.java @@ -2,9 +2,10 @@ 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; @@ -53,7 +54,10 @@ 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 +69,21 @@ public static void main(String[] args) throws IOException { new Point(2, -1)); System.out.println(Point.sort(points)); } + + @Override + public int compareTo(Point point) { + if(this.xpoint.x){ + return 1; + } + if(this.ypoint.y){ + return 1; + } + return 0; + } }