【问题标题】:Calling superclass in main method在 main 方法中调用超类
【发布时间】:2020-05-27 11:07:30
【问题描述】:

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


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;

    }

}

【问题讨论】:

    标签: java polymorphism subclass superclass


    【解决方案1】:

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

    为避免此类错误,请始终在被覆盖的方法中包含@Override annotation。如果您包含该注解并在方法签名中出错,则编译将失败。

    另请注意,这两种方法的返回类型不同(String 和 void)。被覆盖的方法必须具有兼容的返回类型。

    【讨论】:

    • 谢谢!那行得通!我考虑过使用@Override,但不确定这是否需要。非常感谢!我真的很感激:)
    【解决方案2】:

    有一些拼写错误(employeeSummary vs. EmployeeSummary)和返回类型不匹配,在 Employee 中应该是

    public void 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);   
    }
    

    然后在管理器中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-30
      • 2019-03-24
      • 2021-11-10
      • 2014-03-26
      • 2014-04-29
      • 2012-09-28
      • 2011-03-28
      • 2016-02-06
      相关资源
      最近更新 更多