封装继承多态练习

package com.baidu.demo06;
 
import java.util.Scanner;
 
//所有员工的父类
public class Employee {
    private int monthN = 6;//假设现在六月
    private int month;
    private int salary = 100;
 
    public Employee() {
        setMonth(month);
    }
 
    public int getMonth() {
        return month;
    }
 
    public void setMonth(int month) {
        this.month = month;
    }
 
    public double getSalary() {
        if (monthN == month) {
            return salary;
        } else return 0;
    }
}
package com.baidu.demo06;
 
public class SalariedEmployee extends Employee {
 
    private double salary;
 
    public SalariedEmployee(double salary) {
        // TODO Auto-generated constructor stub
        super();
        setSalary(salary);
    }
 
    @Override
    public double getSalary() {
        return salary;
    }
 
    public void setSalary(double salary) {
        this.salary = salary;
    }
}

package com.baidu.demo06;
 
public class HourlyEmployee extends Employee {
 
    private double hourlySalary;
    private int hour;
 
    public HourlyEmployee(double hourlySalary, int hour) {
        super();
        setHour(hour);
        setHourlySalary(hourlySalary);
    }
 
    @Override
    public double getSalary() {
        if (hour > 160)
            return 160 * hourlySalary + (hour - 160) * 1.5 * hourlySalary;
        else
            return hour * hourlySalary;
    }
 
    public double getHourlySalary() {
        return hourlySalary;
    }
 
    public void setHourlySalary(double hourlySalary) {
        this.hourlySalary = hourlySalary;
    }
 
    public int getHour() {
        return hour;
    }
 
    public void setHour(int hour) {
        this.hour = hour;
    }
 
 
}

package com.baidu.demo06;
 
public class SalesEmployee extends Employee {
 
 
    private double sale;
 
    private double addRate;
 
 
    public SalesEmployee(double sale, double addRate) {
        setAddRate(addRate);
        setSale(sale);
 
    }
 
    @Override
 
 
    public double getSalary() {
        // TODO Auto-generated method stub
        return sale * addRate;
 
    }
 
 
    public double getSale() {
        return sale;
 
    }
 
 
    public void setSale(double sale) {
        this.sale = sale;
 
    }
 
 
    public double getAddRate() {
        return addRate;
 
    }
 
 
    public void setAddRate(double addRate) {
        this.addRate = addRate;
 
    }
 
}
package com.baidu.demo06;
 
public class BasePlusSaleEmployee extends SalesEmployee {
 
    private double baseSalary;
    private double addRate;
    private double sale;
 
    public BasePlusSaleEmployee(double sale, double addRate, double baseSalary) {
        super(sale, addRate);
        // TODO Auto-generated constructor stub
        setAddRate(addRate);
        setBaseSalary(baseSalary);
        setSale(sale);
    }
 
    @Override
    public double getSalary() {
        SalesEmployee a = new SalesEmployee(sale, addRate);
        // TODO Auto-generated method stub
        return baseSalary + a.getSalary();
    }
 
 
    public double getBaseSalary() {
        return baseSalary;
    }
 
    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }
 
    public double getAddRate() {
        return addRate;
    }
 
    public void setAddRate(double addRate) {
        this.addRate = addRate;
    }
 
    public double getSale() {
        return sale;
    }
 
    public void setSale(double sale) {
        this.sale = sale;
    }
 
 
}
package com.baidu.demo06;
 
import java.util.Calendar;
 
public class Text {
    public static void main(String[] args) {
        Employee employee[] = {
                new SalariedEmployee(1000),
                new HourlyEmployee(16, 150),
                new SalesEmployee(500, 0.2),
                new BasePlusSaleEmployee(500, 0.2, 1000)
        };
        Calendar cal = Calendar.getInstance();
        cal.set(2018, 6, 18);
        for (int i = 0; i < employee.length; i++) {
            int month = cal.get(Calendar.MONTH);
            Employee a = new Employee();
            a.setMonth(month);
            double welfare = a.getSalary();
            double salary = employee[i].getSalary();
            System.out.println("第" + (i + 1) + "个:" + (welfare + salary));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值