• 某公司的雇员分为以下若干类


    /**
     * 某公司的雇员分为以下若干类:
     * Employee:这是所有员工总的父类,其属性包括:员工姓名,员工的生日
     *                               其方法包括:getSalary(int month)
     *                               根据月份来确定工资,如果该月员工过生日,则公司额外奖励100元。
     *
     * SalariedEmployee:Employee的子类,拿固定工资的员工,属性:月薪
     *
     * HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超过160小时的部分按照1.5倍工资发放
     *                               属性:每小时的工资,每月工作的小时数
     *
     * SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率来决定
     *                               属性:月销售额、提成率
     *
     * BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加销售提成
     *                               属性:底薪
     * 根据要求创建SalariedEmployee、HourlyEmployee、SalesEmployee和BasePlusSalesEmployee
     * 并计算某个月这四个对象的工资
     * 要求:每个类都实现封装,不允许有非私有化属性。
     */
    class EmployeeText{
        public static void main(String[] args) {
            //创建对象
            //拿固定工资的员工
            Employee worker1 = new SalariedEmployee("张三",11,5000);
            //按小时拿工资的员工
            Employee worker2 = new HourlyEmployee("李四",11,20,240);
            //销售人员
            Employee worker3 = new SalesEmployee("王五",11,300000,0.1);
            //有固定底薪的销售人员
            Employee worker4 = new BasePlusSalesEmployee("赵六",11,90000,0.1,1000);
            System.out.println("拿固定工资的员工11月的工资是:" + worker1.getSalary(11));
            System.out.println("按小时拿工资的员工11月的工资是:" + worker2.getSalary(11));
            System.out.println("销售人员11月的工资是:" + worker3.getSalary(11));
            System.out.println("有固定底薪的销售人员11月的工资是:" + worker4.getSalary(11));
        }
    }
    /*
    程序运行结果:
        拿固定工资的员工11月的工资是:5100.0
        按小时拿工资的员工11月的工资是:5700.0
        销售人员11月的工资是:30000.0
        有固定底薪的销售人员11月的工资是:10100.0
    */
    //父类
    class Employee{
        //属性
        private String name;
        private  int birthday;
        //无参构造
        public Employee() {
        }
        //有参构造
        public Employee(String name, int birthday) {
            this.name = name;
            this.birthday = birthday;
        }
        //set and get
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getBirthday() {
            return birthday;
        }
    
        public void setBirthday(int birthday) {
            this.birthday = birthday;
        }
    
        //获取员工工资的方法
        public double getSalary(int month){
            return 0;   //具体实现在子类重写该方法的时候提供
        }
    }
    
    //拿固定工资的员工类
    class SalariedEmployee extends Employee{
        //子类特有属性
        private double yueXin;
        //无参构造
        public SalariedEmployee() {
        }
    
        public SalariedEmployee(String name, int birthday) {
            //调用父类中有参构造
            super(name, birthday);
        }
    
        public SalariedEmployee(String name, int birthday, double yueXin) {
            //调用父类中有参构造
            super(name, birthday);
            //给子类中特有属性赋值
            this.yueXin = yueXin;
        }
        //get方法
        public double getYueXin() {
            return yueXin;
        }
        //set方法
        public void setYueXin(double yueXin) {
            this.yueXin = yueXin;
        }
    
        //重写父类中的方法
        @Override   //@Override是注解,表示下面的方法是重写父类中的方法
        public double getSalary(int month) {
            //如果该月员工过生日,工资增加100元
            if (this.getBirthday() == month){
                return this.getYueXin() + 100;
            }else {
                return this.getYueXin();
            }
        }
    }
    
    //按小时拿工资的员工类
    class HourlyEmployee extends Employee{
        //时薪属性
        private double hourSalary;
        //工作时长属性
        private double hour;
    
        //无参构造
        public HourlyEmployee() {
        }
    
        //有参构造
        public HourlyEmployee(String name, int birthday, double hourSalary, double hour) {
            super(name, birthday);
            this.hourSalary = hourSalary;
            this.hour = hour;
        }
    
        //set and get
        public double getHourSalary() {
            return hourSalary;
        }
    
        public void setHourSalary(double hourSalary) {
            this.hourSalary = hourSalary;
        }
    
        public double getHour() {
            return hour;
        }
    
        public void setHour(double hour) {
            this.hour = hour;
        }
    
        //重写方法
        @Override
        public double getSalary(int month) {
            //指定一个变量
            double salary = 0;
            //如果工作时间小于160小时
            if (this.getHour() <= 160){
                salary = this.getHourSalary() * this.getHour();
            }else { //工作时间大于160小时
                salary = this.getHourSalary() * 160 + 1.5 * this.getHourSalary() * (this.getHour() - 160);
            }
            //如果工人这个月过生日
            if (this.getBirthday() == month){
                //工资增加100元
                salary += 100;
            }
            return salary;
        }
    }
    //SalesEmployee 销售人员类
    class SalesEmployee extends Employee{
        //属性:月销售额、
        private double sale;
        //提成率
        private double royalty;
    
        //无参构造
        public SalesEmployee() {
        }
    
        //有参构造
        public SalesEmployee(String name, int birthday, double sale, double royalty) {
            super(name, birthday);
            this.sale = sale;
            this.royalty = royalty;
        }
        //get and set
    
        public double getSale() {
            return sale;
        }
    
        public void setSale(double sale) {
            this.sale = sale;
        }
    
        public double getRoyalty() {
            return royalty;
        }
    
        public void setRoyalty(double royalty) {
            this.royalty = royalty;
        }
        //重写父类中的方法
    
        @Override
        public double getSalary(int month) {
            return this.sale * this.royalty;
        }
    }
    //BasePlusSalesEmployee 有固定底薪的销售人员类
    class BasePlusSalesEmployee extends SalesEmployee{
        private double basicSalary;
    
        //无参构造方法
        public BasePlusSalesEmployee() {
        }
    
        //有参构造方法
        public BasePlusSalesEmployee(String name, int birthday, double sale, double royalty, double basicSalary) {
            super(name, birthday, sale, royalty);
            this.basicSalary = basicSalary;
        }
    
        //set and get 方法
        public double getBasicSalary() {
            return basicSalary;
        }
    
        public void setBasicSalary(double basicSalary) {
            this.basicSalary = basicSalary;
        }
    
        //重写父类方法
        @Override
        public double getSalary(int month) {
            if (this.getBirthday() == month){
                return this.getSale() * this.getRoyalty() + this.getBasicSalary() + 100;
            }else {
                return this.getSale() * this.getRoyalty() + this.getBasicSalary();
            }
        }
    }
  • 相关阅读:
    CoinGecko 播客:与 Cartesi 联合创始人 Erick 一起构建 Layer-2
    数据库补充知识
    A-Level经济真题(11)
    Spring原理解析
    家里Win7电脑如何连接公司Win10电脑?快解析+远程桌面
    怎样用 Python数据 写一个自动交易的股票程序接口?
    [点云分割] 条件欧氏聚类分割
    STM32H7使用FileX库BUG,SD卡挂载失败
    如何判断mysql innodb buffer pool内存配置不足
    初识变量和数据类型
  • 原文地址:https://blog.csdn.net/heliuerya/article/details/128055277