这个例子中应用了abstract关键字去体现 模板方法的设计模式。
package oop.exer10; public abstract class Employee { private String name; private int number; private MyDate birthday; public Employee(String name, int number, MyDate birthday) { super(); this.name = name; this.number = number; this.birthday = birthday; } 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 "name=" + name + ", number=" + number + ", birthday=" + birthday.toDateString() ; } }
package oop.exer10; public class MyDate { private int year; private int month; private int day; public MyDate() { super(); } 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+"日"; } }
package oop.exer10; public class HourlyEmployee extends Employee{ private int wage;//每小时的工资 private int hour;//月工作小时数 public HourlyEmployee(String name, int number, MyDate birthday,int wage,int hour) { super(name, number, birthday); this.wage=wage; this.hour=hour; } 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; } public String toString() { return "HourlyEmployee ["+super.toString()+"]"; } }
package oop.exer10; public class SalariedEmployee extends Employee{ private double monthlySalary;//月工资 public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } public SalariedEmployee(String name, int number, MyDate birthday,double monthlySalary) { super(name, number, birthday); // TODO Auto-generated constructor stub this.monthlySalary=monthlySalary; } @Override public double earnings() { return monthlySalary; } public String toString() { return "SalariedEmployee ["+super.toString()+"]"; } }
package oop.exer10; import java.util.Calendar; import java.util.Scanner; public class PayrollSystem { public static void main(String[] args) { //方式一 // Scanner scan=new Scanner(System.in); // System.out.println("请输入当月的月份"); // int month = scan.nextInt(); //方式二 Calendar calendar = Calendar.getInstance(); int month=calendar.get(Calendar.MONTH)+1;//获取当前的月份 一月份是0 Employee[] emps=new Employee[2]; //此处new的是数组而不是new的abstract类型的Employee对象,数组元素类型是Employee类型 emps[0]=new SalariedEmployee("Tom", 1001, new MyDate(1998, 11, 27),10000); emps[1]=new HourlyEmployee("Jerry", 1002, new MyDate(2000, 12, 28),60,240); for(int i=0;i欢迎分享,转载请注明来源:内存溢出
评论列表(0条)