Наследование Java - не удается найти конструктор символов - PullRequest
3 голосов
/ 11 февраля 2011

Я не могу понять, как скомпилировать мой подкласс, даже если он вызывает конструктор суперклассов?

Этот класс не будет компилироваться:

package departments;
import employees.*;

public class DepartmentEmployee extends Employee{

    private Department department;

    public DepartmentEmployee(Profile profile, Address address, Occupation occupation,   Department department) {
        assert (department != null) : "invalid Department";
        super(profile, address, occupation);
        this.department = department;
    }

}

суперкласс:

package employees;

public class Employee {

    protected Profile profile;
    protected Address address;
    protected Occupation occupation;

    protected Employee(Profile profile, Address address, Occupation occupation) {
        assert (profile != null) : "invalid Profile";
        assert (address != null) : "invalid Address";
        assert (occupation != null) : "invalid Occupation";
        this.profile = profile;
        this.address = address;
        this.occupation = occupation;
    }

}

Подкласс продолжает говорить "не может найти символ - конструктор Сотрудник".Два в разных пакетах.Правильно ли я их связал?

1 Ответ

6 голосов
/ 11 февраля 2011

super() должен быть первым вызовом в конструкторе. Переставь его выше assert.

Смотри также:

Вызов конструктора суперкласса должен быть первой строкой в ​​конструкторе подкласса.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...