Employee类的层级结构

本文介绍了如何使用Java设计Employee类及其子类SalariedEmployee、HourlyEmployee和CommissionEmployee,详细阐述了它们的数据成员、构造方法、设置和读取方法,以及如何通过测试程序实例化并输出对象信息。
摘要由CSDN通过智能技术生成

Employee类的层级结构

定义四个类,分别为Employee类、SalariedEmployee类、HourlyEmployee类和CommissionEmployee类。其中Employee类是其他三个类的父类。Employee类包含姓名和身份证号。除此之外,SalariedEmployee类还应包含每月工资;HourlyEmployee类还应包含每小时工资数和工作时间数;CommissionEmployee还应包含提成比例和销售总额。其中HourlyEmployee的工资为:每小时工资数×工作时间数,CommissionEmployee的工资为:提成比例×销售总额。每个类都应有合适的构造方法、数据成员的设置和读取方法。编写一个测试程序,创建这些类的对象,并输出与对象相关的信息。注意子类有时需调用父类的构造方法和被覆盖的方法,成员变量定义为private,对有些方法实现重载。测试程序如下所示:

public static void main(String [] args){
   Scanner in=new Scanner(System.in);
   Employee [] e=new Employee[3];
   e[0]=new SalariedEmployee(in.next(),in.next(),in.nextDouble());
   e[1]=new HourlyEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
   e[2]=new CommissionEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
   for(int i=0;i<e.length;i++)
   {    
       System.out.println(e[i].getSalary());
       System.out.println(e[i]);
   }
}

输入格式:
输入三行。第一行为一个SalariedEmployee对象的姓名,身份证号和每月工资。第二行为一个HourlyEmployee对象的姓名、身份证号、每小时工资数、工作时间。第三行为一个CommissionEmployee对象的姓名、身份证号、提成比例和销售总额。

输出格式:
输出三个对象的工资和对象的其他信息。每一个对象输出两行,第一行为工资,第二行为对象的信息。

输入样例:
Mike 0001 5000
Jack 0002 20 300
Tom 0003 0.2 50000
输出样例:
5000.0
SalariedEmployee[name=Mike,id=0001][monthSalary=5000.0]
6000.0
HourlyEmployee[name=Jack,id=0002][hourSalary=20.0,workhour=300.0]
10000.0
CommissionEmployee[name=Tom,id=0003][commissionRatio=0.2,sale=50000.0]

代码实现

 
 
import java.util.*;
 
class Employee{
	protected String name;
	protected String id;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Employee() {
		
	}
	public Employee(String name, String id) {
		this.name = name;
		this.id = id;
	}
	public double getSalary() {
		return 0;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ",id=" + id + "]";
	}
	
}
 
class SalariedEmployee extends Employee{
	private double monthSalary;
 
	public double getmonthSalary() {
		return monthSalary;
	}
 
	public void setmonthSalary(double monthSalary) {
		this.monthSalary = monthSalary;
	}
	public double getSalary() {
		return this.monthSalary;
	}
	public SalariedEmployee(String name, String id ,double monthSalary) {
		super(name, id);
		this.monthSalary = monthSalary;
	}
 
	@Override
	public String toString() {
		return "SalariedEmployee[name=" + name + ",id=" + id + "]"+"[monthSalary=" + monthSalary + "]";
	}
	
}
 
class HourlyEmployee extends Employee{
	private double hourSalary;
	private double workhour;
	public double gethourSalary() {
		return hourSalary;
	}
	public void sethourSalary(double hourSalary) {
		this.hourSalary = hourSalary;
	}
	public double getworkhour() {
		return workhour;
	}
	public void setworkhour(double workhour) {
		this.workhour = workhour;
	}
	public double getSalary() {
		return this.hourSalary*workhour;
	}
	public HourlyEmployee(String name, String id ,double hourSalary, double workhour) {
		super(name, id);
		this.hourSalary = hourSalary;
		this.workhour = workhour;
	}
	@Override
	public String toString() {
		return "HourlyEmployee[name=" + name + ",id=" + id + "]"+"[hourSalary=" + hourSalary + ",workhour=" + workhour + "]";
	}
}
 
class CommissionEmployee extends Employee{
	private double commissionRatio;
	private double sale;
	public double getcommissionRatio() {
		return commissionRatio;
	}
	public void setcommissionRatio(double commissionRatio) {
		this.commissionRatio = commissionRatio;
	}
	public double getsale() {
		return sale;
	}
	public void setsale(double sale) {
		this.sale = sale;
	}
	public double getSalary() {
		return this.commissionRatio*this.sale;
	}
	public CommissionEmployee(String name, String id,double commissionRatio, double sale) {
		super(name,id);
		this.commissionRatio = commissionRatio;
		this.sale = sale;
	}
	@Override
	public String toString() {
		return "CommissionEmployee[name=" + name + ",id=" + id + "]"+"[commissionRatio=" + commissionRatio + ",sale=" + sale + "]";
	}
}
 
public class Main{
    public static void main(String [] args){
        Scanner in=new Scanner(System.in);
        Employee [] e=new Employee[3];
        e[0]=new SalariedEmployee(in.next(),in.next(),in.nextDouble());
        e[1]=new HourlyEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
        e[2]=new CommissionEmployee(in.next(),in.next(),in.nextDouble(),in.nextDouble());
        for(int i=0;i<e.length;i++)
        {    
            System.out.println(e[i].getSalary());
            System.out.println(e[i]);
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值