简体   繁体   中英

setName and received always null

I'm new in Java:] and I have a little problem with my app: Why when I run it, it keeps saying me "null" even when person.setname("John") I tried to fix it but without good result, what is wrong here and why? I tried to debug it - same result - setName set name to John but anyway it keeps printing "null" Really strange for new user like me. If someone can help, or even try to say me what's wrong i'd be glad, thanks.


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);
    }
}

You have person object containing another person called "target" and another one "target":

在此处输入图像描述

Blacklist should look like this:

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

You were creating one entry: "Bill, Adam, Jessie".

You were doing some unnecessary metods override, adding unnecessary objects - when you extend class you have that object "person" already there, you don't need to put it as another object "target". If you want to have both null check and blacklist check executed before setting name you can extend classes in hierarchy: Person -> PersonWithNullCheck -> PersonWithBlacklistedCheck. Now setName() method will be executed in each of classes as ordered. Here's a fixed solution:

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 + '\'' +
                '}';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM