java-面向对象中级-习题(设计父类一员工类)

1.设计父类一员工类。子类:工人类(Worker),农民类(Peasant), 教师类(Teacher),科学家类
(Scientist),服务生类(Waiter)。
(1)其中工人,农民,服务生只有基本工资
(2)教师除基本工资外,还有课酬(元/天)
(3)科学家除基本工资外,还有年终奖
(4)编写一个测试类,将各种类型的员工的全年工资打印出来

Homework5

package com.hspedu.homework.homework05;

public class Homework5 {
    public static void main(String[] args) {
        Worker jack = new Worker("jack", 100);
        jack.setSalMonth(10);
        jack.printSal();
        Peasant smith = new Peasant("smith", 150);
        smith.printSal();
        Teacher amir = new Teacher("Amir", 300);
        amir.setClassDays(360);
        amir.setClassSal(100);
        amir.printSal();
        Scientist scientist = new Scientist("钟南山", 20000);
        scientist.setBonus(2000000);
        scientist.printSal();
    }
}

Employee

package com.hspedu.homework.homework05;

public class Employee {
    private String name;
    private double salary;
    private int salMonth = 12;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public int getSalMonth() {
        return salMonth;
    }

    public void setSalMonth(int salMonth) {
        this.salMonth = salMonth;
    }

    public void printSal(){
        System.out.println(name + " 年工资是:" + (salary*salMonth));
    }
}

Worker

package com.hspedu.homework.homework05;

public class Worker extends Employee{
    public Worker(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void printSal() {
        System.out.print("工人");
        super.printSal();
    }
}

Peasant

package com.hspedu.homework.homework05;

public class Peasant extends Employee{
    public Peasant(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void printSal() {
        System.out.print("农民");
        super.printSal();
    }
}

Teacher

package com.hspedu.homework.homework05;

public class Teacher extends Employee{
    private int classDays;
    private double classSal;

    public Teacher(String name, double salary) {
        super(name, salary);
    }

    public int getClassDays() {
        return classDays;
    }

    public void setClassDays(int classDays) {
        this.classDays = classDays;
    }

    public double getClassSal() {
        return classSal;
    }

    public void setClassSal(double classSal) {
        this.classSal = classSal;
    }

    @Override
    public void printSal() {
        System.out.println("老师" + getName() + " 年工资是:"
        +(getSalary() * getSalMonth() + classSal * classDays) );
    }
}

Scientist

package com.hspedu.homework.homework05;

public class Scientist extends Employee{
    private double bonus;

    public Scientist(String name, double salary) {
        super(name, salary);
    }

    public double getBonus() {
        return bonus;
    }

    public void setBonus(double bonus) {
        this.bonus = bonus;
    }

    @Override
    public void printSal() {
        System.out.println("科学家" + getName() +" 年工资是:" +
                (getSalary()*getSalMonth()+getBonus()));
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值