构造函数Teacher(Teacher)未定义

时间:2019-03-14 18:53:55

标签: java inheritance composition

我在名为“部门”的类中使用复制构造函数和继承来从“老师”类(它是“人”的子类)中调用信息。创建我的set / get方法后,出现上述错误。任何人都知道为什么会发生这种情况吗?

“部门”类中的代码:

public class Department {
private String deptName;
private int numMajors;
private Teacher[] listTeachers; //inherits from Person class
private Student[] listStudents; //inherits from Person class

    // First constructor for Department
    public Department(String dn, int nm, Teacher[] listTeachers, Student[] listStudents) {
    this.deptName = dn;
    this.numMajors = nm;

    this.listTeachers = new Teacher[listTeachers.length];
    for (int i = 0; i < this.listTeachers.length; i++)
    {
        this.listTeachers[i] = new Teacher (listTeachers[i]);
    }

    //set method for Teachers Array
    public void setListTeachers (Teacher[] other) {
    this.listTeachers = new Teacher[other.length];
    for (int i = 0; i < listTeachers.length; i++) {
        this.listTeachers[i] = new Teacher (other[i]);
    }
}

    //get method for Teachers Array
    public Teacher[] getListTeachers() {
    Teacher[] copyTeachers = new Teacher[listTeachers.length];
    for (int i = 0; i < copyTeachers.length; i++) {
        copyTeachers[i] = new Teacher(this.listTeachers[i]);
    }
    return copyTeachers;
}

以下是给我错误的行:

1)this.listTeachers[i] = new Teacher (listTeachers[i]);

2)this.listTeachers[i] = new Teacher (other[i]);

3)copyTeachers[i] = new Teacher(this.listTeachers[i]);

“老师”课程中的代码:

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

//Constructor for use in Teacher main method.
public Teacher(String n, int a, String s, boolean al, String i, int sal, int numyr) {
    super(n, a, s, al);
    this.id = i;
    this.salary = sal;
    this.num_yr_prof = numyr;
}

//Copy constructor for use in Department class.
public Teacher (String n, int a, String s, boolean al, Teacher other) {
    super(n, a, s, al);
    if (other == null) {
        System.out.println("Fatal Error!");
        System.exit(0);
    }

    this.id = other.id;
    this.salary = other.salary;
    this.num_yr_prof = other.num_yr_prof;
}

2 个答案:

答案 0 :(得分:1)

您需要在Teacher类中使用一个接受Teacher的构造函数:

const doFunction = () => {
  const s = document.createElement('script');
  s.type = 'text/javascript';
  s.src = 'https://myscript';
  s.id = 'abc';

  document.body.appendChild(s);
}

test('doFunction', () => {
  jest.spyOn(document.body, 'appendChild');

  doFunction();

  expect(document.body.appendChild).toBeCalledWith(
    expect.objectContaining({
      type: 'text/javascript',
      src: 'https://myscript/',
      id: 'abc'
    })
  ); // Success!
});

答案 1 :(得分:1)

您的副本构造函数可能如下所示:

    public Teacher(Teacher teacher) {
        this( teacher.n, teacher.a, teacher.s, teacher.al, 
              teacher.id, teacher.salary, teacher.num_yr_prof );
    }

由于您没有显示Person类的代码,因此我在这里使用了变量名n,a,s和al。应该用Person类中命名的那些变量替换它们。当然,这假定这些变量是公共变量或受保护变量。如果它们是私有的,则需要为这些变量使用getter(即使它们是公共的或受保护的,还是首选的)。