java - 在main方法中调用父类(super class)

标签 java polymorphism subclass superclass

我刚刚了解了父类(super class)和子类,作业非常简单:有 2 个类和一个测试类来调用和打印属性。下面是我所有 3 个类的代码。我的问题是,为什么部门属性没有打印在我的主目录中?其他一切都打印得很好,我只是无法打印最后一点。我认为这与 super 有关...提前谢谢您!第二门计算机类(class),我终于感觉自己有点明白了,所以这比我参加的第一个类有所进步!


public class Employee {

    private String firstName;
    private String lastName;
    private int employeeID;
    private double salary;

    public Employee () {

        firstName = null;
        lastName = null;
        employeeID = 0;
        salary = 0.00;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getEmployeeID() {
        return employeeID;
    }

    public double getSalary() {
        return salary;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmployeeID(int employeeID) {
        this.employeeID = employeeID;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public String employeeSummary () {
        String employeeSummary = "Employee's name is: " + getFirstName() + " " + getLastName() +
                    ". The employee's ID number is " + getEmployeeID() + 
                    ". The employee's salary is " + getSalary();
        System.out.println(employeeSummary);
        return employeeSummary;
    }

}


public class Manager extends Employee {

    private String departmentA;

    public Manager() {
        super();
        departmentA = null;
    }

    public String getDepartmentA() {
        return departmentA;
    }

    public void setDepartmentA(String departmentA) {
        this.departmentA = departmentA;
    }

    public void EmployeeSummary() {
        super.employeeSummary();
        System.out.println("The employee's department is " + departmentA);
    }
}


public class ManagerDerivation {

    public static void main(String[] args) {
        Manager person = new Manager();

        person.setFirstName("Ron");
        person.setLastName("Weasley");
        person.setEmployeeID(2345);
        person.setSalary(65000.00);
        person.setDepartmentA("Department of Magical Law Enforcement");
        person.employeeSummary();

    return;

    }

}

最佳答案

方法名称区分大小写。 EmployeeSummary() 不会覆盖 employeeSummary(),因为它使用不同的名称。

为避免此类错误,请始终包含 @Override annotation关于重写的方法。如果您包含该注释并在方法签名中犯了错误,编译将失败。

另请注意,这两种方法的返回类型不同(Stringvoid)。重写的方法必须具有兼容的返回类型。

关于java - 在main方法中调用父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60179067/

相关文章:

oop - 为什么维基百科说 "Polymorphism is not the same as method overloading or method overriding."

java - 子类需要有构造函数吗?

java - 创建子类时出现 StackOverflowError

java - null 替代返回整数的方法

java - 我无法解决尝试在空对象引用上调用虚拟方法 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'

haskell - 输入函数的签名来查找向量的大小

haskell - 如何根据用户定义的数据类型编写函数返回整数或 bool 值?

python - 避免 if __name__ == '__main__' 在 Python 子类中使用来自父类的函数运行

java - OpenJPA 将另一表中的数据插入到一个表中

java - 不使用 BigInteger 的 Karatsuba 算法