简体   繁体   English

为什么 getName() 返回 null?

[英]Why does getName() returns null?

EDIT: Thanks for all the answers!编辑:感谢所有的答案! I didn't know that the object p in List.java is different from the p in Main.java.我不知道 List.java 中的 object p与 Main.java 中的p不同。 I passed it as a parameter and it works fine now.我将它作为参数传递,现在它工作正常。 Thank you!谢谢!

In Main.java:在 Main.java 中:

        System.out.println("Enter your name:");
        String name = scan.next();
        name+=scan.nextLine();

        String words[]=name.split("\\s");  
        String capitalizeWord="";  
        for(String w:words){  
            String first=w.substring(0,1);  
            String afterfirst=w.substring(1);  
            capitalizeWord+=first.toUpperCase()+afterfirst+" ";  
        }

        Person p = new Person(capitalizeWord);

In Person.java亲自.java

    private String name;
    private int age;

    Person(String newName){
        name=newName;
    }

    Person(int newAge){
        age=newAge;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

In List.java:在 List.java 中:

public void printInvoice(){
        System.out.println("Enter your age:");
        int age = scan.nextInt();

        Person p = new Person(age);
        System.out.println("Thank you for shopping with us, "+p.getName());
}

The last output is最后一个 output 是

Thank you for shopping with us, null

I don't know why this is happening.我不知道为什么会这样。 Did I do something wrong?我做错什么了吗? I think the code is correct?我认为代码是正确的?

You have two constructors.你有两个构造函数。 One that accepts a string, one that accepts a number.一种接受字符串,一种接受数字。

In your latest example you called the one that accepts a number and, since there's nothing initializing the name member, it gets initialized to null as default.在您的最新示例中,您调用了一个接受数字的示例,并且由于没有初始化name成员,因此默认情况下它被初始化为null

The p in Main.java has nothing to do with the p in List.java (they're two different variables), so initializing the former will have no effect on the latter. Main.java 中的pMain.java中的p List.java (它们是两个不同的变量),因此初始化前者对后者没有影响。

Each time you call a constructor you get a distinct instance of the object.每次调用构造函数时,都会得到 object 的不同实例。 The object created in Main.java with a name is stored in a local variable p that is only existing in the scope of Main.java . The object created in Main.java with a name is stored in a local variable p that is only existing in the scope of Main.java . In List.java you create a second object with an age, but no name.List.java ,您创建第二个 object,但没有名称。 It's also stored in a variable called p but that one is in the scope of List.java only and has nothing to do with the earlier created object.它也存储在一个名为p的变量中,但该变量仅在 List.java 的List.java中,与之前创建的 object 无关。

It sounds like you want to add the name to the earlier object and not create a new one.听起来您想将名称添加到较早的 object 而不是创建新名称。 For that you should pass the first object as a parameter to the code that adds the age, perhaps like this:为此,您应该将第一个 object 作为参数传递给添加年龄的代码,可能像这样:

public void addAge(Person p) {
  System.out.println("Enter your age:");
  int age = scan.nextInt();
  p.setAge(age);  // will have to make this method in the Person class
  System.out.println("Thank you for shopping with us, "+p.getName());
}

Calling Person p = new Person(age);呼叫Person p = new Person(age); gives a brand new Person object that has no name yet.给出了一个还没有名字的全新 Person object。

As I can see in your List.java class, you have instantiated a new Person object with age as a parameter, so for that object, the name would be null , since the two reference variables are pointing to the different Person object As I can see in your List.java class, you have instantiated a new Person object with age as a parameter, so for that object, the name would be null , since the two reference variables are pointing to the different Person object

The first reference p to object Person has instantiated the person object using the contructor:- object Person的第一个引用p已使用构造函数实例化了 person object:-

Person(String name) {
    this.name = name;
}

Second reference p to object Person has instantiated the person object using the constructor:-第二个引用p到 object Person已经使用构造函数实例化了 person object:-

Person(int age) {
    this.age = age;
}

You should have used setter to set the property age to an already created Person object您应该使用 setter 将属性age设置为已创建的Person object

code that create your person -> Person p = new Person(age);创建你的person的代码 -> Person p = new Person(age);

constructor called -> Person(int newAge){ age=newAge; }构造函数调用 -> Person(int newAge){ age=newAge; } Person(int newAge){ age=newAge; } This constructor let the name with his default value.. so null此构造函数让名称具有他的默认值.. null Person(int newAge){ age=newAge; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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