From 2d5c5b36612ddbe443db6e9af4f06b59ac578690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E5=87=AF=E6=B3=A2?= <1069977596@qq.com> Date: Tue, 6 Aug 2019 19:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=90=E6=A0=87=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 | 27 +++++++++++++++++-- 1 file changed, 25 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..b55cae2 100644 --- a/src/main/java/com/github/hcsp/polymorphism/Point.java +++ b/src/main/java/com/github/hcsp/polymorphism/Point.java @@ -2,9 +2,11 @@ import java.io.IOException; import java.util.Arrays; +import java.util.Collection; +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 +55,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 +70,22 @@ public static void main(String[] args) throws IOException { new Point(2, -1)); System.out.println(Point.sort(points)); } + + + + @Override + public int compareTo(Point that) { + if (this.xthat.x){ + return 1; + } + if (this.ythat.y){ + return 1; + } + return 0; + + } }