在 main 方法中调用超类
Calling superclass in main method
我刚刚学习了 superclasses 和 subclasses,作业很简单:有 2 个 classes 和一个测试 class调用并打印属性。以下是我所有 3 classes 的代码。我的问题是,为什么部门属性没有打印在我的主要文件中?其他一切都打印得很好,我只是无法打印最后一点。我认为这与超级有关......提前谢谢你!第二门计算机课程,我终于觉得我有点明白了,所以这是我参加的第一个 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。如果包含该注释并在方法签名中出错,编译将失败。
另请注意,这两种方法的 return 类型不同(String
和 void
)。覆盖的方法必须具有兼容的 return 类型。
存在一些拼写错误(employeeSummary 与 EmployeeSummary)并且 return 类型不匹配,在 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);
}
我刚刚学习了 superclasses 和 subclasses,作业很简单:有 2 个 classes 和一个测试 class调用并打印属性。以下是我所有 3 classes 的代码。我的问题是,为什么部门属性没有打印在我的主要文件中?其他一切都打印得很好,我只是无法打印最后一点。我认为这与超级有关......提前谢谢你!第二门计算机课程,我终于觉得我有点明白了,所以这是我参加的第一个 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。如果包含该注释并在方法签名中出错,编译将失败。
另请注意,这两种方法的 return 类型不同(String
和 void
)。覆盖的方法必须具有兼容的 return 类型。
存在一些拼写错误(employeeSummary 与 EmployeeSummary)并且 return 类型不匹配,在 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);
}