抽象类和abstract关键字的使用

编写工资系统,实现不同类型员工(多态)的按月发放工资 。如果当月出现某个Employee 对象的生日,则将该雇员的工资增加100元 。

实验说明:

(1)定义一个 Employee 类,该类包含:private成员变量 name,number,birthday,其中 birthday 为 MyDate 类的对象;abstract方法 earnings();toString方法输出对象的name,number和birthday。

public abstract class Employee {
     private String name;
     private int number;
     private MyDate birthday;
     public Employee(){
    	 
     }
     public Employee(String name,int number,MyDate birthday){
    	 this.birthday=birthday;
    	 this.name=name;
    	 this.number=number;
     }
     
     public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public MyDate getBirthday() {
		return birthday;
	}
	public void setBirthday(MyDate birthday) {
		this.birthday = birthday;
	}
	public abstract double earnings();
	@Override
	public String toString() {
		return "Employee [name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() + "]";
	}

}

(2)  MyDate 类包含private成员变量 year month,day;toDateString方法返回日期对应的字符串: xxxx 年 xx 月 xx 日

public class MyDate {
      private int year;
      private int month;
      private int day;
      
      public MyDate(int year, int month, int day) {
		super();
		this.year = year;
		this.month = month;
		this.day = day;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

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

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public String toDateString(){
    	  return year+"年"+month+"月"+day+"日";
      }
}

(3)定义 SalariedEmployee 类继承 Employee 类,实现按月计算工资的员工处理。该类包括private 成员变量 monthlySalary实现父类的抽象方法earnings(), 该方法返回 monthlySalary 值;toString() 方法输出员工类型信息及员工name,number,birthday。

public class SalariedEmployee extends Employee {
     private double monthlySalary;
     public SalariedEmployee(){
    	 
     }
     public SalariedEmployee(String name,int number,MyDate birthday,double monthlySalary){
    	 super(name, number, birthday);
    	 this.monthlySalary=monthlySalary;
     }
     
	public double getMonthlySalary() {
		return monthlySalary;
	}
	public void setMonthlySalary(double monthlySalary) {
		this.monthlySalary = monthlySalary;
	}
	@Override
	public double earnings() {
          return monthlySalary;
	}
	@Override
	public String toString() {
		return "SalariedEmployee["+super.toString()+"]";
	}
}

 (4)参照 SalariedEmployee 类定义 HourlyEmployee 类,实现按小时计算工资的员工处理。该类包括:private成员变量 wage 和 hour实现父类的抽象方法earnings(), 该方法返回 wage*hour 值;toString方法输出员工类型信息及员工name,number,birthday.

public class HourlyEmployee extends Employee {
     private int wage;//每小时工资
     private int hour;//月工作的小时数
     public HourlyEmployee(){
    	 
     }
     public HourlyEmployee(String name,int number,MyDate birthday,int wage,int hour){
    	 super(name, number, birthday);
    	 this.hour=hour;
    	 this.wage=wage;
     }
     
	public int getWage() {
		return wage;
	}
	public void setWage(int wage) {
		this.wage = wage;
	}
	public int getHour() {
		return hour;
	}
	public void setHour(int hour) {
		this.hour = hour;
	}
	@Override
	public double earnings() {
		return wage*hour;
	}
	@Override
	public String toString(){
		return "HourlyEmployee["+super.toString()+"]";
	}
     
}

(5)定义 PayrollSystem 类,创建 Employee 变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型 ,name,number, 以及该对象生日。当键盘输入本月月份值时,如果本月是某个 Employee 对象的生日,还要输出增加工资信息。

import java.util.Scanner;

public class PayrollSystem {
public static void main(String[] args) {
	Scanner scanner=new Scanner(System.in);
	System.out.println("input momth:");
	int month=scanner.nextInt();
	Employee[] emps=new Employee[2];
	emps[0]=new SalariedEmployee("amy", 1, new MyDate(1998, 1, 1), 5000);
	emps[1]=new HourlyEmployee("tom", 2, new MyDate(1998, 2, 1), 700, 10);
	for(int i=0;i<emps.length;i++){
		System.out.println(emps[i].toString());
		System.out.println("月工资为"+emps[i].earnings());
		if(month==emps[i].getBirthday().getMonth()){
			System.out.println("happy birthday!奖励100元");		
		}
	}
}
}

上一篇:关于继承和抽象类的笔记


下一篇:抽象类和接口