【问题标题】:Java - My super.toString is returning nullJava - 我的 super.toString 返回 null
【发布时间】:2018-02-15 16:25:10
【问题描述】:

我在 Department 中的 toString 方法返回空值时遇到问题,似乎 super.toString 没有获取输入的值。我不确定代码哪里出错了,我知道要整理很多,我试图摆脱任何不重要的代码。谢谢参观。抱歉,如果我的结构有误。

public class Person {
String name;
private int age;
private String ssn;
private boolean alive;

public Person(String name, int age, String ssn, boolean alive) {
    this.name = name;
    this.age = age;
    this.ssn = ssn;
    this.alive = alive;
}

public String toString() {
    return "Name: " + this.getName() + "\n" +
            "Age: " + this.getAge() + "\n" +
            "SSN: " + this.getSsn() + '\n' +
            "Alive: " + this.isAlive();
}
}

教师扩展人

public class Teacher extends Person {
private String id;
private int salary;
private int num_yr_prof;

public Teacher(String name, int age, String ssn, boolean alive,           String id, int salary, int num_yr_prof) {
    super(name, age, ssn, alive);
    this.id = id;
    this.salary = salary;
    this.num_yr_prof = num_yr_prof;
}
public Teacher(Teacher t) {
    this.id = t.id;
    this.salary = t.salary;
    this.num_yr_prof = t.num_yr_prof;
}

@Override
public String toString() {
    return super.toString() +
            "\nID: " + getId() +
            "\nSalary: $" + getSalary() +
            "\nYears as a prof: " + getNum_yr_prof();
}
}

这个类使用一组教师

public class Department {
private String deptName;
private int numMajors;
private Teacher[] listTeachers;

public Department(String dN, int nM, Teacher[] lT) {
    this.deptName = dN;
    this.numMajors = nM;
    this.listTeachers = new Teacher[lT.length];
    for (int i = 0; i <lT.length ; i++) {
        listTeachers[i] = new Teacher(lT[i]);
    }
}

public String toString() {
    String output = "Department: ";
    output += "\n   Name: " + getDeptName();
    output += "\n   Majors: " + getNumMajors();
    output += "\n   Teachers: \n";
    for (int i = 0; i < listTeachers.length; i++) {
        output += listTeachers[i].toString(); //This is returning null values
    }
    return output;
}
}

主要方法

public class lab4prt2 {
public static void main(String[] args) {
    Teacher[] teachers = new Teacher[]{
            new Teacher(
                    "Tracy",
                    36,
                    "39890280",
                    true,
                    "4859279",
                    100000,
                    5)
    };

    Department d1 = new Department("Math", 4, teachers);

    System.out.println(d1.toString());

}
}

【问题讨论】:

  • 此代码无法编译。 Teacher(Teacher t) 构造函数不会调用超级构造函数,或者它会(隐式)调用并且您没有在Person 中显示无参数构造函数。
  • 在这种情况下,那个无参数构造函数是做什么的?我猜它不会初始化它的任何实例字段。

标签: java tostring super


【解决方案1】:

乍一看,在我看来,Department 构造函数调用了带有参数 Teacher 的 Teacher 构造函数。

在这个构造函数中,super() 没有被调用,也没有编译,所以我猜你可能在复制粘贴的过程中遗漏了一些东西。

无论如何,我担心这是一个非常低效的代码,因为您从数组开始重新创建 Teacher 对象,最重要的是,您丢失了部分信息。

【讨论】:

  • 谢谢,我没有错过任何明显的错误对我复制代码的方式造成的影响。我的教授希望我们在调用对象时重新创建对象,他说它太隐藏了信息的位置。你能解释一下为什么效率低吗?
  • 您有一个 main 方法,您可以在其中创建一组 Teacher 对象。然后将此数组作为 Department 构造函数的参数传递。这个对象有一个 Teacher[],但不是将传递的参数分配给它,而是通过迭代传递的数组来重新创建每个对象。这意味着您存储的信息是您可能只是参考的信息的两倍。我知道这可能是一个练习,因为在现实生活中你会依赖关系数据库,但在我看来,没有必要拥有 2 次相同的不可变对象实例。希望这会有所帮助:)
猜你喜欢
  • 1970-01-01
  • 2015-06-04
  • 2019-08-25
  • 2016-05-28
  • 2016-03-08
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多