diff --git a/src/main/java/com/github/hcsp/polymorphism/User.java b/src/main/java/com/github/hcsp/polymorphism/User.java index 7a601df..6220af6 100644 --- a/src/main/java/com/github/hcsp/polymorphism/User.java +++ b/src/main/java/com/github/hcsp/polymorphism/User.java @@ -47,7 +47,10 @@ public int hashCode() { /** 老板说让我按照用户名排序 */ @Override public int compareTo(User o) { - return name.compareTo(o.name); + if(name.compareTo(o.name) == 0) { + return id.compareTo(o.id); + } + return name.compareTo(o.name); } public static void main(String[] args) { @@ -56,7 +59,7 @@ public static void main(String[] args) { new User(100, "b"), new User(10, "z"), new User(1, "a"), - new User(2000, "a")); + new User(2000, "a2")); TreeSet treeSet = new TreeSet<>(users); // 为什么这里的输出是3?试着修复其中的bug System.out.println(treeSet.size());