利用多态Cola公司的雇员分类

Cola公司的雇员分类


1. ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
2. SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪。
3. HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数。
4. SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率。
5. 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

代码如下(示例):

public class HomeWork {


		public static void main(String[] args) {

	    	Company com = new Company();
	    	//固定工子类
	    	SalariedEmployee emp = new SalariedEmployee("张三",12, 2000);
	    	                //实参
//	    	com.printSalary(emp, 12);
	    	//小时工
	    	HourlyEmployee  he = new HourlyEmployee("李四", 11, 100, 161);
//	    	com.printSalary(he, 11);
	    	
	    	//销售员
	    	SalesEmployee se = new SalesEmployee("王五", 10,100000,0.2);
//	    	com.printSalary(se, 10);
	    	
	    	ColaEmployee[] arr = new ColaEmployee[3];
	    	arr[0]=emp;
	    	arr[1]=he;
	    	arr[2]=se;
	    	
	    	for(int i=0; i<arr.length; i++) {
	    		com.printSalary(arr[i], 11);
	    	}
	    	
	    }
	}

	//父类
	class ColaEmployee {
		String name;
		int birth_month;
		
		double getSalary(int month) {
			return 0.0;
		}
	}

	//固定工子类
	class SalariedEmployee extends ColaEmployee {
		double salary;
		//构造器
		SalariedEmployee(String name, int birth_month, double salary) {
			this.name=name;
			this.birth_month=birth_month;
			this.salary=salary;
		}
		
		//方法的覆盖
		double getSalary(int month) {
			if(this.birth_month == month)
				salary +=100;
			return salary;
		}
	}

	//小时工子类
	class HourlyEmployee extends ColaEmployee {
		double salary;
		double hour_salary;
		int month_hour;
		
		//构造器
		HourlyEmployee(String name, int birth_month, double hour_salary, int month_hour) {
			this.name=name;
			this.birth_month=birth_month;
			this.hour_salary=hour_salary;
			this.month_hour=month_hour;
		}
		
		//方法的覆盖(重写)
		double getSalary(int month) {
			if(month_hour <=160) {
				salary = hour_salary *  month_hour;
			} else {
				salary = hour_salary *  160 + hour_salary * 1.5 * (month_hour - 160);
			}
			if(this.birth_month == month)
				salary +=100;
			
			return salary;
		}
	}


	//销售员子类
	class SalesEmployee extends ColaEmployee {
		double salary;
		double month_quota;
		double rate;
		
		//构造器
		SalesEmployee(String name, int birth_month, double month_quota, double rate) {
			this.name=name;
			this.birth_month=birth_month;
			this.month_quota=month_quota;
			this.rate=rate;
		}
		
		//方法的覆盖
		double getSalary(int month) {
			salary = this.month_quota *  this.rate;
			if(this.birth_month == month)
				salary +=100;
			
			return salary;
		}
	}

	class Company {
		                  //形參 父类 使用多态的特性
		//                父类  使用多态的特性
		void printSalary(ColaEmployee emp, int month) {
//			    emp.getSalary(month);
			         
			System.out.println(emp.getSalary(month));
		}
	}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值