[英]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?我是 Java 的新手:] 我的应用程序有一点问题:为什么当我运行它时,即使person.setname("John")
我尝试修复它但没有好的结果,它仍然说我"null"
,这里有什么问题,为什么? 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.我试图调试它 - 结果相同 - setName
将名称设置为 John 但无论如何它一直打印“null”对于像我这样的新用户来说真的很奇怪。 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":你有一个人 object 包含另一个人称为“目标”和另一个人“目标”:
Blacklist should look like this:黑名单应如下所示:
List<String> blacklist = Arrays.asList("Bill", "Adam", "Jessie");
You were creating one entry: "Bill, Adam, Jessie".您正在创建一个条目:“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".您正在执行一些不必要的方法覆盖,添加不必要的对象 - 当您扩展 class 时,您已经拥有 object“人”,您不需要将其作为另一个 object“目标”。 If you want to have both null check and blacklist check executed before setting name you can extend classes in hierarchy: Person -> PersonWithNullCheck -> PersonWithBlacklistedCheck.如果您想在设置名称之前同时执行 null 检查和黑名单检查,您可以在层次结构中扩展类:Person -> PersonWithNullCheck -> PersonWithBlacklistedCheck。 Now setName() method will be executed in each of classes as ordered.现在 setName() 方法将按顺序在每个类中执行。 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 + '\'' +
'}';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.