Worker小Demo

小Demo

题目:

  1. 工人类的属性:工龄、工号、姓名、基本工资、岗位级别
  2. 输出所有信息的方法
  3. 定义一个输出工资的方法(工资=基本工资)
  4. 创建一个部门经理类,继承工人Worker类,并重写其计算工资的方法(工资=基本工资+岗位> 级别500+工龄1000)
  5. 创建一个销售人员类,继承工人Worker类,并增加一个属性(销售金额)。并重写其计算工资的方法

(工资=基本工资+销售金额*系数(其中系数当销售金额大于100W时为0.09,小于100W时为0.08))

  1. 创建测试类,分别定义以上三种类的对象,并赋值,调用其输出工资的方法进行测试。

根据题目要求可知,一共需要创建三个类和一个测试类

  • 工人类代码如下:
public class Worker {
//    **工人类**的属性:工龄、工号、姓名、基本工资、岗位级别
    private int age;
    private int id;
    private String name;
    private int baseSalary;
    private int grade;

    public Worker() {
    }

    public Worker(int age, int id, String name, int baseSalary, int grade) {
        this.age = age;
        this.id = id;
        this.name = name;
        this.baseSalary = baseSalary;
        this.grade = grade;
    }

    //输出所有信息
    public void showAllInfo(){
        System.out.println(this.name+"的id是"+this.id+",的工资是"+this.baseSalary+"工龄为"+this.id+",岗位级别是"+this.grade);
    }

//    定义一个输出工资的方法(工资=基本工资)
    public void showSalary(){
        System.out.println("工资是"+this.baseSalary);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(int baseSalary) {
        this.baseSalary = baseSalary;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }
}

  • 经理类代码如下

    public class Manager extends Worker{
    
            public Manager() {
            }
    
            public Manager(int age, int id, String name, int baseSalary, int grade) {
                super(age, id, name, baseSalary, grade);
            }
    
            @Override
            public void showSalary() {
                int Gz = super.getBaseSalary() + super.getGrade() * 500 + super.getAge() * 1000;
                System.out.println("工资是"+Gz);
            }
        }
    
  • 销售人员类代码如下;

    public class Sales extends Worker{
    
        public Sales(int saleroom) {
            this.saleroom = saleroom;
        }
    
        public Sales(int age, int id, String name, int baseSalary, int grade, int saleroom) {
            super(age, id, name, baseSalary, grade);
            this.saleroom = saleroom;
        }
    
        /**
         * 创建一个销售人员类,继承工人Worker类,并增加一个属性(销售金额),并重写其计算工资的方法
         * (工资=基本工资+销售金额*系数
         * (其中系数当销售金额大于100W时为0.09,小于100W时为0.08))
         */
         private int saleroom;
    
        @Override
        public void showSalary() {
            double i = 0.0;
            if (saleroom >= 100_0000){
                i = 0.09;
            }else {
                i = 0.08;
            }
            double Gz = super.getBaseSalary() + saleroom * i;
            System.out.println("销售的工资是"+Gz);
        }
    
        public int getSaleroom() {
            return saleroom;
        }
    
        public void setSaleroom(int saleroom) {
            this.saleroom = saleroom;
        }
    }
    
    
  • 测试类代码如下

    //创建测试类,分别定义以上三种类的对象,并赋值,调用其输出工资的方法进行测试。
    public class TestWorker {
        public static void main(String[] args) {
            Worker worker = new Worker(18,10086,"北屿",13500,5);
            Manager manager = new Manager(18,10086,"北屿",13500,5);
            manager.showSalary();
            System.out.println();
            Sales sales = new Sales(180_0000);
            sales.showSalary();
        }
    }
    

运行以上代码后控制台截图如下:

Worker小Demo

题目比较简单,小编就不做过多解释啦,有问题欢迎评论区留言哦~

上一篇:dig和nslookup的区别与简单应用


下一篇:剑指 Offer 27. 二叉树的镜像