Java的封装、继承和多态

这周老师布置了一个Java的小作业,所以就把代码分享给大家吧!

 

一、 实验目的: 
1、理解封装机制,掌握各种修饰符的使用。 
2、理解继承机制,掌握继承的应用。 
3、体会并掌握 this、super 的使用。 
二、实验内容:

某公司的员工分为以下若干类:

1、Employee:所有员工总的父类:

属性:姓名,出生月份。

方法:getSalary (int month)如果该月员工过生日,公司额外奖励 100 元。

2、SalariedEmployee:Employee 的子类,拿固定工资的员工。

属性:姓名,出生月份、月薪  

方法:重写父类的 getSalary(),计算出应发工资后应加上 super.getSalary()。

3、HourlyEmployee:Employee 的子类,按小时拿工资的员工,每月工作超出 160 小时 的部分按照 1.5 倍工资发放。

属性:姓名,出生月份,每月工作的小时数、每小时的工资。

方法:重写父类的 getSalary(),计算出应发工资后应加上 super.getSalary()。

4、SalesEmployee:Employee 的子类,销售人员,工资由月销售额和提成率决定。

属性: 月属性:姓名,出生月份,销售额、提成率。

方法:重写父类的 getSalary()方法,计算出应发工资后应加上 super.getSalary()。

5、BasePlusSalesEmployee:SalesEmployee 的子类,有固定底薪的销售人员,工资由底 薪加上销售提成部分。

属性:姓名,出生月份,销售额、提成率,底薪。 方法:重写父类 SalesEmployee 的 getSalary()方法,计算出应发工资后应加上 super.getSalary()。 

package Employee;

public class Employee{
	private String name;
	private int month;
	
	Employee(){}
	Employee(String name, int month){
		
		this.name = name;
		this.month = month;
	}

	public void setName(String name){
		this.name = name;
	}

	public String getName(){
		return name;
	}

	public void setMouth(int month){
		this.month = month;
	} 

	public int getMouth(){
		return month;
	}

	public double getSalary(int month){
		if (month == this.month)
		{
			return 100;
		}
		else
		{
			return 0;
		}
	}

}


//以下是SalariedEmployee类
package Employee;

class SalariedEmployee extends Employee
{
	private double monthSalary;   //月薪
	
	public SalariedEmployee() {}
	public SalariedEmployee(String name, int month, int monthSalary) {
		super(name, month);
		this.monthSalary = monthSalary;
	}

	public void setMonthSalary(double monthSalary) {
		this.monthSalary = monthSalary;
	}
	
	public double getMonthSalary() {
		return monthSalary;
	}
	
	
	@Override
	public double getSalary(int month){
		return monthSalary + super.getSalary(month);  
	}
	

}

///以下是BasePlusSalesEmployee类
package Employee;

public class BasePlusSalesEmployee extends SalesEmployee {

	double baseSalary;
	
	BasePlusSalesEmployee(){}

	BasePlusSalesEmployee(String name,int month,int monthSales, double rate,double baseSalary){
		super(name,month,monthSales,rate);
		this.baseSalary = baseSalary;
	}

	public void setBaseSalary(double baseSalary){
		this.baseSalary = baseSalary;
	}
	
	public double getBaseSalary(){
		return baseSalary;
	}

	@Override
	public double getSalary(int month){
		
		return baseSalary+super.getSalary(month);
	}
}

/以下是HourlyEmployee类
package Employee;

public class HourlyEmployee extends Employee{

	private int hour;
	private double hourPay;

	HourlyEmployee(){}
	HourlyEmployee(String name,int month,int hour, double hourPay){
		super(name, month);
		this.hour = hour;
		this.hourPay = hourPay;
	}


	public void setHour(int hour){
		this.hour = hour;
	}
	
	public int getHour(){
		return hour;
	}

	public void setHourPay(double hourPay){
		this.hourPay = hourPay;
	}

	public double getHourPay(){
		return hourPay;
	}

	@Override
	public double getSalary(int month){
		if (hour > 160)
		{
			return (float) (hourPay * 160 + (hour - 160) * hourPay * 1.5 
					+ super.getSalary(month));
		}
		else
		{
			return hourPay * hour + super.getSalary(month);
		}


	}
}

///以下是SalesEmployee类
package Employee;

public class SalesEmployee extends Employee{

	private int monthSales;
	private double rate;

	SalesEmployee(){};

	SalesEmployee(String name,int month,int monthSales, double rate){
		super(name,month);
		this.monthSales = monthSales;
		this.rate = rate;
	}
	
	@Override
	public double getSalary(int month){
		return monthSales * rate + super.getSalary(month);

	}
}

以下是Test运行
package Employee;

public class EmployeeTest{
	public static void main(String[] args){
		Employee a[] = new Employee[4];
		a[0] = new SalariedEmployee("张三", 2, 1000);
		a[1] = new HourlyEmployee("李四",3,20,200);
		a[2] = new SalesEmployee("王五",4,50000,(float)0.1);
		a[3] = new BasePlusSalesEmployee("刘六",5,50000,(float)0.1,1000);

		System.out.println(String.format("固定工资员工:张三的工资为:%.2f",a[0].getSalary(2)));
		System.out.println(String.format("小时工员工:李四的工资为:%.2f",a[1].getSalary(2)));
		System.out.println(String.format("销售员员工:王五的工资为:%.2f",a[2].getSalary(2)));
		System.out.println(String.format("固定底薪销售员:刘六的工资为:%.2f",a[3].getSalary(2)));
	}
}

因为老师要求要用setter和getter方法,所以我就没删除,如果大家不需要,可以自己手动删除一下。 

运行结果:

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值