练习14——抽象类应用的练习

it2022-05-05  121

1、Employee父类

package test4; /** * 〈一句话功能简述〉<br> * 〈〉 * * @author abu * @create 2019/7/18 * @since 1.0.0 */ public abstract class Employee { private String name; private int number; private MyDate1 birthday; public abstract double earnings(); @Override public String toString() { return "Employee{" + "name='" + name + '\'' + ", number=" + number + ", birthday=" + birthday.toDateString() + '}'; } public Employee() { super(); } public Employee(String name, int number, MyDate1 birthday) { 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 MyDate1 getBirthday() { return birthday; } public void setBirthday(MyDate1 birthday) { this.birthday = birthday; } }

2、SalariedEmployee类

/** * 〈一句话功能简述〉<br> * 〈〉 * * @author abu * @create 2019/7/18 * @since 1.0.0 */ public class SalariedEmployee extends Employee{ private double monthlySalary; public double earnings(){ return monthlySalary; } @Override public String toString() { return "SalariedEmployee{" + super.toString() + "monthlySalary=" + monthlySalary + '}'; } public SalariedEmployee(String name, int number, MyDate1 birthday, double monthlySalary) { super(name, number, birthday); this.monthlySalary = monthlySalary; } public double getMonthlySalary() { return monthlySalary; } public void setMonthlySalary(double monthlySalary) { this.monthlySalary = monthlySalary; } }

3、HourlyEmployee类

/** * 〈一句话功能简述〉<br> * 〈〉 * * @author abu * @create 2019/7/18 * @since 1.0.0 */ public class HourlyEmployee extends Employee { private double wage; private double hour; @Override public double earnings() { return wage*hour; } public HourlyEmployee(String name, int number, MyDate1 birthday, double wage, double hour) { super(name, number, birthday); this.wage = wage; this.hour = hour; } @Override public String toString() { return "HourlyEmployee{" +super.toString() + "wage=" + wage + ", hour=" + hour + '}'; } public double getWage() { return wage; } public void setWage(double wage) { this.wage = wage; } public double getHour() { return hour; } public void setHour(double hour) { this.hour = hour; } }

4、MyDate类

/** * 〈一句话功能简述〉<br> * 〈〉 * * @author abu * @create 2019/7/18 * @since 1.0.0 */ public class MyDate1 { private int year; private int month; private int day; public MyDate1(int year, int month, int day) { super(); this.year = year; this.month = month; this.day = day; } public String toDateString(){ return year + "年" + month + "月" + 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; } }

5、PayrollSystem测试类

import java.util.Scanner; /** * 〈一句话功能简述〉<br> * 〈〉 * * @author abu * @create 2019/7/18 * @since 1.0.0 */ public class PayrollSystem { public static void main(String[] args){ Employee[] emps = new Employee[4]; emps[0] = new SalariedEmployee("坂田银时", 1001,new MyDate1(1995,07,01),3000 ); emps[1] = new SalariedEmployee("志村新八", 1002, new MyDate1(1992,04,26),1500); emps[2] = new SalariedEmployee("神乐",1003, new MyDate1(1996,01,01),3500); emps[3] = new HourlyEmployee("夏目贵志" ,1004,new MyDate1(1993,01,01),100,8); Scanner s = new Scanner(System.in); System.out.println("这是几月呀~"); int month = s.nextInt(); for(int i = 0; i < emps.length; i ++){ if(month == emps[i].getBirthday().getMonth()){ System.out.println("这个月给" + emps[i].getName() + "加工资啦!"); System.out.println(emps[i]); }else { System.out.println("这个月不给" + emps[i].getName() + "加工资"); } } } }

最新回复(0)