我应该做的事情: 编写一个java程序来显示继承概念,其中Employee是超类,而Manager是子类。遵循以下类结构: - Employee类
变量:
构造函数 - 使用它来初始化函数
之上的所有变量Display() – This displays all the 3 variable values.
setSalary() – This sets the salary variable.
getSalary() – This gets the salary variable.
到目前为止我所拥有的是什么
public class Employee {
private int employeeNumber;
private String employeeName;
private double employeeSalary;
public Employee(int employeeNumber, String employeeName, double employeeSalary) {
this.employeeNumber = employeeNumber;
this.employeeName = employeeName;
this.employeeSalary = employeeSalary;
}
}
我的问题是如何实现这三种方法? 他们只是标准的吸气剂和制定者吗? 我不熟悉显示功能,如果有人可以提供帮助的话 谢谢!
答案 0 :(得分:0)
你真的很亲密。您需要创建管理器类并扩展Employee类。但首先让我们添加描述的方法来设置薪水并显示所有字段!
员工类: display()方法将字段连接在一起,将它们分开。
((CoqAst
(VernacDefineModule ()
(Id Backport_WSets)
(((Id E)))
(CMident
(Ser_Qualid (DirPath ((Id DecidableType))) (Id DecidableType))
DefaultInline)
(()
(((Id M)))
((CMwith
(CMwith
(CMident
(Ser_Qualid (DirPath ((Id MSetInterface))) (Id WSets))))
(CWith_Definition
(((Id E) (Id t)))
(CRef
(Qualid
((Ser_Qualid (DirPath ((Id E))) (Id t))))
()))
(CWith_Definition
(((Id E) (Id eq)))
(CRef
(Qualid
((Ser_Qualid (DirPath ((Id E))) (Id eq))))
()))
DefaultInline)))
(Check
(CMapply
(CMident
(Ser_Qualid (DirPath ((Id FSetInterface))) (Id WSfun))))
((v (CMident (Ser_Qualid (DirPath ()) (Id E))))
DefaultInline)))
()))
经理类:从一般知识来看,我认为经理可以拥有多名员工。 因此,在本课程中,您将拥有管理器的特定方法,例如添加新员工,或显示或为给定管理员工作的员工。
public class Employee {
private int employeeNumber;
private String employeeName;
private double employeeSalary;
public Employee(int employeeNumber, String employeeName, double employeeSalary){
this.employeeNumber=employeeNumber;
this.employeeName=employeeName;
this.employeeSalary=employeeSalary;
}
public void display(){
System.out.println("Employee Number: "+ employeeNumber +"\n"
+ "Employee Name: " + employeeName + "\n"
+ "Employee Salary: " + employeeSalary);
}
public double getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(double employeeSalary) {
this.employeeSalary = employeeSalary;
}
}
测试:请注意,Manager继承了Employee类的所有方法,这就是为什么在Manager类中不需要覆盖方法,但当然可以这样做来添加新功能。
public class Manager extends Employee {
List<Employee> subordinates;
public Manager(int employeeNumber, String employeeName, double employeeSalary) {
super(employeeNumber, employeeName, employeeSalary);
subordinates = new ArrayList<>();
}
public void displayEmployees(){
for(Employee employee: subordinates){
employee.display();
// just print an empty line - so its prettier
System.out.println();
}
}
public void addNewEmployee(Employee employee){
subordinates.add(employee);
}
public List<Employee> getSubordinates() {
return subordinates;
}
public void setSubordinates(List<Employee> subordinates) {
this.subordinates = subordinates;
}
}