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

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值