【问题标题】:setName and received always nullsetName 并收到始终为空
【发布时间】:2020-05-13 06:54:02
【问题描述】:

我是 Java 新手 :] 我的应用程序有一点问题: 为什么当我运行它时,它一直说我"null" 即使person.setname("John") 我试图修复它但没有好的结果,这里有什么问题,为什么? 我试图调试它 - 结果相同 - setName 将名称设置为 John 但无论如何它一直打印“null” 对于像我这样的新用户来说真的很奇怪。 如果有人可以提供帮助,或者甚至尝试告诉我出了什么问题,我会很高兴,谢谢。


class App {
    public static void main(String[] args) throws InterruptedException {
        List<String> blacklist = Arrays.asList("Bill, Adam, Jessie");
        ;
        Person person = new PersonWithBlacklistedCheck(
                new PersonWithNullCheck(new Person()),
                blacklist);
        person.setName("John");
        System.out.println("Person: " + person);
    }
}

class PersonWithBlacklistedCheck extends Person {
    private final List<String> blacklist;
    private final Person target;
    PersonWithBlacklistedCheck(Person target, List<String> blacklist) {
        this.target = target;
        this.blacklist = blacklist;
    }
    @Override
    public String getName() {
        return target.getName();
    }
    @Override
    public void setName(String name) {
        if (this.blacklist.contains(name)) {
            throw new RuntimeException("[" + name + "] cannot be used as a name! it is blacklisted");
        }
        target.setName(name);
    }
}

class Person {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person{" +
               "name='" + name + '\'' +
               '}';
    }
}

class PersonWithNullCheck extends Person {
    private final Person target;
    PersonWithNullCheck(Person target) {
        this.target = target;
    }
    @Override
    public String getName() {
        return target.getName();
    }
    @Override
    public void setName(String name) {
        if (name == null) {
            throw new RuntimeException("[name] must not be null!!");
        }
        target.setName(name);
    }
}

【问题讨论】:

  • 您没有对 Person 使用继承权。在您的每个子类中,您正在创建一个包含在子类中的新 Person。要使用继承,您应该摆脱目标字段并从 Person 继承名称。

标签: java list null


【解决方案1】:

您的 person 对象包含另一个名为“target”的人和另一个名为“target”的人:

黑名单应该是这样的:

List<String> blacklist = Arrays.asList("Bill", "Adam", "Jessie");

您正在创建一个条目:“Bill, Adam, Jessie”。

你做了一些不必要的方法覆盖,添加了不必要的对象——当你扩展类时,你已经有了那个对象“人”,你不需要把它作为另一个对象“目标”。如果您想在设置名称之前同时执行空值检查和黑名单检查,您可以在层次结构中扩展类:Person -> PersonWithNullCheck -> PersonWithBlacklistedCheck。现在 setName() 方法将按顺序在每个类中执行。这是一个固定的解决方案:

public class Test {
    public static void main(String[] args) {
        List<String> blacklist = Arrays.asList("Bill", "Adam", "Jessie");
        Person person = new PersonWithBlacklistedCheck(blacklist);
        person.setName("John");
        System.out.println("Person: " + person);
    }
}

class PersonWithBlacklistedCheck extends PersonWithNullCheck {
    private final List<String> blacklist;

    PersonWithBlacklistedCheck(List<String> blacklist) {
        this.blacklist = blacklist;
    }

    @Override
    public void setName(String name) {
        if (this.blacklist.contains(name)) {
            throw new RuntimeException("[" + name + "] cannot be used as a name! it is blacklisted");
        }
        super.setName(name);
    }
}

class PersonWithNullCheck extends Person {
    @Override
    public void setName(String name) {
        if (name == null) {
            throw new RuntimeException("[name] must not be null!!");
        }
        super.setName(name);
    }
}

class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2019-07-18
    • 2019-08-18
    • 2011-12-29
    • 2019-07-02
    相关资源
    最近更新 更多