gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:00:49 25 4
gpt4 key购买 nike

我刚刚了解了父类(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com