From ac58f870b274bbfe09b8bcb6787d0eae44a97398 Mon Sep 17 00:00:00 2001 From: Ludwig <392559142@qq.com> Date: Tue, 24 Nov 2020 16:42:53 +0800 Subject: [PATCH 1/2] fix --- .../java/com/github/hcsp/polymorphism/User.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/User.java b/src/main/java/com/github/hcsp/polymorphism/User.java index 78d927d..28dc662 100644 --- a/src/main/java/com/github/hcsp/polymorphism/User.java +++ b/src/main/java/com/github/hcsp/polymorphism/User.java @@ -1,13 +1,19 @@ package com.github.hcsp.polymorphism; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Consumer; public class User { - /** 用户ID,数据库主键,全局唯一 */ + /** + * 用户ID,数据库主键,全局唯一 + */ private final Integer id; - /** 用户名 */ + /** + * 用户名 + */ private final String name; public User(Integer id, String name) { @@ -26,9 +32,9 @@ public String getName() { // 这里使用了一个NameCollector类,请尝试将它改写成匿名内部类 // 使得代码更加集中,更加容易阅读 public static List collectNames(List users) { - NameCollector collector = new NameCollector(); - users.forEach(collector); - return collector.getNames(); + List names = new ArrayList<>(); + users.forEach(user -> names.add(user.getName())); + return names; } public static void main(String[] args) { From b445455853f1706cea6040adf01e8f78223778a3 Mon Sep 17 00:00:00 2001 From: Ludwig <392559142@qq.com> Date: Tue, 24 Nov 2020 16:46:42 +0800 Subject: [PATCH 2/2] fix --- src/main/java/com/github/hcsp/polymorphism/User.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/polymorphism/User.java b/src/main/java/com/github/hcsp/polymorphism/User.java index 28dc662..683eed5 100644 --- a/src/main/java/com/github/hcsp/polymorphism/User.java +++ b/src/main/java/com/github/hcsp/polymorphism/User.java @@ -3,7 +3,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.function.Consumer; public class User { /**