简体   繁体   English

构造函数打印出 NULL 而不是对象值

[英]Constructor printing out NULL instead of object value

I created a new class "Lecturer" which extends another class "Person", i wanted to make 2 constructors for Lecturer and one would accept a name and a stipend (just a constant to say how much pay is), the other just accepts the name and uses the default stipend set in the code.我创建了一个新类“Lecturer”,它扩展了另一个类“Person”,我想为 Lecturer 制作 2 个构造函数,一个会接受一个名字和一个津贴(只是一个常数来说明工资是多少),另一个只接受name 并使用代码中设置的默认津贴。 i included appropriate getters and setters.我包括了适当的 getter 和 setter。 I then wrote a writeOutput method to print an output similar to this然后我写了一个 writeOutput 方法来打印一个类似于这个的输出

Name: (name) which gets the name and prints it Stipend: (stipend) same process ^ Name: (name) 获取名称并打印它 Stipend: (stipend) 相同的过程^

heres what i have so far这是我目前所拥有的

Lecturer.java讲师.java

public class Lecturer extends Person{

private static String name;
static double stipend;

public Lecturer(String name) {
    super(name);

}

public Lecturer(String name, double stipend) {
    super(name);
}

public String getName() {
    return name;
}



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



public double getStipend() {
    return stipend;
}



public void setStipend(double stipend) {
    this.stipend = stipend;
}

public static void writeOutput() {
    System.out.println("Name: " + name);
    System.out.println("Stipend: " + stipend);
    }

 }

Person.java人.java

public class Person {

/** Every Person has a name */
private String name;

/** Person requires a name */
public Person(String n) {
    this.name = n;
}

/** return this Person's name */
public String getName() {
    return this.name;
}

/** Change this Person's name */
public void setName(String nn) {
    this.name = nn;
}

Main file (Inheritance.java)主文件(Inheritance.java)

Lines 41-53第 41-53 行

    Lecturer l1 = new Lecturer("Zachary");
    Lecturer l2 = new Lecturer("Wilhelmina", 11017.00);
    l1.writeOutput();
    l2.writeOutput();
    pause();
    l1.setName("Zack");
    l1.setStipend(10800.00);
    l1.writeOutput();
    pause();
    System.out.printf("%s's stipend is $%,4.2f.\n", 
                        l1.getName(), l1.getStipend());        
    System.out.printf("%s's stipend is $%,4.2f.\n", 
                        l2.getName(), l2.getStipend());    

This is the output这是输出

Name: null
Stipend: 0.0
Name: null
Stipend: 0.0

press enter...

Name: Zack
Stipend: 10800.0

The 2nd part works as it should but the first one isnt and i tried to change the code but nothing is working properly.第二部分正常工作,但第一部分不是,我试图更改代码但没有任何工作正常。

In Lecturer you are declaring another name variable.Lecturer您声明了另一个name变量。 This variable is separate from the name variable declared in Person .此变量与Person声明的name变量是分开的。 The call to the superclass constructor is setting the name variable in Person , not in Lecturer .对超类构造函数的调用是在Person设置name变量,而不是在Lecturer But you don't need the second variable;但是您不需要第二个变量; remove it.去掉它。 You can access the name in Person via the getName method you've already declared.您可以通过已经声明的getName方法访问Person的名称。 This means that you also don't need to re-declare getName and setName in Lecturer , so the Lecturer class can inherit them.这意味着您也不需要在Lecturer重新声明getNamesetName ,因此Lecturer类可以继承它们。

Also, in Lecturer , the two variables you've declared shouldn't be static .此外,在Lecturer ,您声明的两个变量不应该是static Per the above reasoning, name shouldn't even be there, but even if it should be there, it shouldn't be static .根据上述推理, name甚至不应该在那里,但即使它应该在那里,也不应该是static The variable stipend should be there, but it shouldn't be static .变量stipend应该在那里,但它不应该是static When you declare a member variable static , then there is only one variable for the entire class, no matter how many instances you create, which doesn't sound like what you want.当你声明一个成员变量static ,整个类只有一个变量,不管你创建了多少个实例,这听起来不像你想要的。

Your constructors should initialize stipend .你的构造函数应该初始化stipend

You have a static variable inside Lecturer which has the same name as the inherited one from Person and your getter is referring to that static one - are you sure you want these static variables?您在 Lecturer 中有一个静态变量,它与从 Person 继承的变量同名,而您的 getter 指的是那个静态变量 - 您确定要这些静态变量吗? For completeness if you really want to keep the static one and the inherited one with the same name then change your getter to read return this.name;为了完整起见,如果你真的想保留静态的和继承的同名的,那么改变你的 getter 读取return this.name; which will return the inherited name instance variable.... But that method can be inherited from Person class...这将返回继承的名称实例变量......但该方法可以从 Person 类继承......

There are two name fields in your program , one is private static String name;你的程序中有两个name字段,一个是private static String name; in Lecturer.java and another is private String name;Lecturer.java 中,另一个是private String name; in person.java .person.java

The thing is that you are just calling Lecturer javs's name field but not setting it.问题是您只是在调用 Lecturer javs 的name字段而不是设置它。

Fixed the project based on rgettman answer.修复了基于 rgettman 答案的项目。

Lecturer class should look like this: Lecturer班应该是这样的:

public class Lecturer extends Person {
    double stipend = 9144;

    public Lecturer(String n) {
        super(n);
    }

    public Lecturer(String n, double stipend) {
        super(n);
        this.stipend = stipend;
    }

    public double getStipend() {
        return stipend;
    }

    public void setStipend(double stipend) {
        this.stipend = stipend;
    }

    public void writeOutput() {
        System.out.println("Name: " + this.getName());
        System.out.println("Stipend: " + getStipend());
    }
}

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

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