java:继承题练习
在公司中,不同类型的员工有不同的薪资计算方式。通过继承的思想,设计一个员工类层次结构。要求:
1.创建一个父类 Employee,该类包含以下成员:
name:员工的姓名;id:员工的ID(假设为整数类型);baseSalary:员工的基本薪资
getSalary() 方法,返回员工的薪资。对于 Employee 类,薪资就是 baseSalary。
2.创建三个子类:
Manager(经理):除了基本薪资外,还会有一个固定的奖金,经理的总薪资(基本薪资 + 奖金)。
Developer(开发员):除了基本薪资外,开发员根据项目完成情况可以获得绩效奖金,开发员的总薪资(基本薪资 + 绩效奖金)。
Intern(实习生):实习生的薪资比基本薪资低,实习生的薪资(基本薪资的某个比例)。
3.创建一个 Company 类,该类包含一个 employees 数组,用来存储所有员工信息,提供方法输出所有员工的基本信息和薪资。
4.创建一个 测试类Test1;输出所有员工的信0息和薪资
package java继承题;
public class Employee {
protected String name;
protected int ID;
protected double baseSalary;
//构造
public Employee() {
}
public Employee(String name, int ID, double baseSalary) {
this.name = name;
this.ID = ID;
this.baseSalary = baseSalary;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setID(int ID) {
this.ID = ID;
}
public int getID(int ID) {
return ID;
}
public void setSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public double getSalary() {
return baseSalary;
}
}
package java继承题;
public class Developer extends Employee{
// Developer(开发员):除了基本薪资外,
// 开发员根据项目完成情况可以获得绩效奖金,
// 开发员的总薪资(基本薪资 + 绩效奖金)。
private double JXJJ;//绩效奖金
public Developer(String name,int ID, double baseSalary, double JXJJ)
{
super(name, ID, baseSalary);
this.JXJJ=JXJJ;
}
@Override
public double getSalary()
{
return super.getSalary()+JXJJ;
}
}
package java继承题;
public class Intern extends Employee{
//(实习生):实习生的薪资比基本薪资低,实习生的薪资(基本薪资的某个比例)
private double BL=0.5;//比例可以设置为final
public Intern(String name,int ID,double baseSalary,double BL){
super(name, ID, baseSalary);
this.BL=BL;
}
@Override
public double getSalary()
{
return (super.getSalary())*BL;
}
}
package java继承题;
public class Manager extends Employee {
private double JJ;//奖金
public Manager(String name, int ID, double baseSalary, double JJ) {
super(name, ID, baseSalary);
this.JJ = JJ;
}
@Override
public double getSalary() {
return super.getSalary() + JJ;
}
}
package java继承题;
public class Company {
/*
3.创建一个 Company 类,该类包含一个 employees 数组,用来存储所有员工信息,提供方法输出所有员工的基本信息和薪资。
*/
private Employee[] employees;
public Company(Employee[] employees)
{
this.employees=employees;
}
public void printEmployee()
{
for(Employee employee:employees)
{
System.out.println("Name:"+employee.getName()+",ID:"+employee.getID()+",baseSalary:"+employee.getSalary());
}
}
}
package java继承题;
public class Test1 {
public static void main(String[] args) {
Employee[]employees = new Employee[]{
new Employee("杨学",38,20000),
new Manager("张莹莹",9477,100000,2000),
new Intern("张三",10,+10000,+0.5),
new Developer("李四",4,10000,100)
};
Company company=new Company(employees);
company.printEmployee();
}
}
原文地址:https://blog.csdn.net/2303_81073778/article/details/143748253
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!