m0_69388073 2022-09-07 07:42 采纳率: 100%
浏览 131
已结题

用java解答1.定义一个员工类,内有姓名与年龄,有一个抽象方法计算其总薪水,

用java解答
1定义一个员工类,内有姓名与年龄,有一个抽象方法计算其总薪水,有个私有静态属性参数统计创建了多少个员工类对象,有个公有方法获取员工类对象个数,有个构造函数初始化参数,并累加个数。
2.定义一个运动类继承了员工类也继承该接口,运动员类有基本薪水与职位薪水属性,其总薪水为基本薪水加职位薪水,如果总薪水不在3000至70000之间则抛出一个Exception异常;如果年龄大于45则表示要退役即退休,如果年龄大于130,抛出一个自定义异常,还有2个构造函数。
3.再定义一个销售员类,他继承了员工类,销售员类有个属性销售数量,其销售数量乘100再加800作为其总薪水,如果总薪水小于800,抛出一个Exception异常;如果年龄大于65则表示退休,还有2个构造函数。

  • 写回答

3条回答 默认 最新

  • 不可描述的两脚兽 2022-09-08 11:36
    关注
    
    import java.util.LinkedList;
    import java.util.List;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    public abstract class Employee {
        
        private String name;
        private int age;
        private static List<Employee> employees = new LinkedList<>();
        
        abstract int salary() throws Exception;
        
        public Employee(String name, int age){
            this.name = name;
            this.age = age;
            employees.add(this);
        }
    
        public String getName() {
            return name;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    class Player  extends Employee{
    
        private int baseSalary;
        private int jobSalary;
        
        public Player (String name, int age, int baseSalary, int jobSalary) {
            super(name, age);
            this.baseSalary = baseSalary;
            this.jobSalary = jobSalary;
        }
    
        @Override
        public int salary() throws Exception {
            int sal = baseSalary + jobSalary;
            if(!(3000<sal && sal<70000)){
                throw new Exception("salary is not between 3000 and 70000.");
            }
            return 0;
        }
        
        public boolean isRetire() throws AgeException {
            int age = super.getAge();
            if(age>130){
                throw new AgeException(age);
            }
            return age > 45;
        }
        
        
    }
    
    class Salesman extends Employee{
        private int salesCount;
        public Salesman(String name, int age) {
            super(name, age);
        }
        
        public Salesman(String name,int age, int salesCount){
            super(name,age);
            this.salesCount = salesCount;
        }
    
        @Override
        int salary() throws Exception {
            int sal = salesCount*100+800;
            if(sal<800){
                throw new Exception("salary less than 800.");
            }
            return sal;
        }
        
        public boolean isRetire(){
            int age = super.getAge();
            return age > 65;
        }
    }
    
    class AgeException extends Exception{
        private int age;
    
        public AgeException(int age){
            super("Wrong Age Exception: " + age);
            this.age = age;
        }
    
        public AgeException(int age, String errMsg){
            super(errMsg);
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了