设计父类——员工类,将各种类型的员工的全年工资打印出来

 设计所需的代码技巧

(1)先死板后灵活

在父类中  salMonth 先默认为12  然后可以在主方法中调用时,teacher.setSalMonth(13);进行修改

同理老师的课酬和上课天数也不一样,用teacher.setClassPay(1000); teacher.setDays(300);修改

调用。

public class Employees { //属性 //* 分析有一个带薪的月份 13, 15,12
private int salMonth = 12; //先默认为12
public class Homerwork03_ {
    public static void main(String[] args) {
teacher.setClassPay(1000);
teacher.setDays(300);
teacher.setSalMonth(13);
teacher.printSal();

 死板的方式:是属性全部构造器,虽然也能修改,但缺乏灵活度,改容易代码报红

    public Teacher(String name, double salary, double classPay, int days) {
        super(name, salary);
        ClassPay = classPay;
        this.days = days;
   }
System.out.println(getName() + " 年工资是 " + ((getSalMonth() * getSalary()) + 
(getDays() * getClassPay())));

灵活的使用方法:是Teacher特有属性暂不构造器,在main中setter调用该属性

//属性
private double ClassPay;
private int days;     //一年上课天数
//构造器
public Teacher(String name, double salary) {
    super(name, salary);
}

//重写printSal()方法
@Override
public void printSal() {
    System.out.print("教师 ");
    System.out.println(getName() + " 年工资是 " + ((getSalMonth() * getSalary()) + (days * ClassPay)));


//main里调用
teacher.setClassPay(1000);
teacher.setDays(300);

(2)学会综合分析

1.工人(Worker)年工作月份;
2.年工资(12薪,13薪,15薪);
3.老师(Teacher)的课酬(ClassPay)和上课天数(Days);
4.科学家(Scientist)的年终奖(bonus)都是可修改的属性,需要注意,这样也可以优化代码。

 代码

1.员工类 Employees.java
package OOPHomework_.Homework_03;

public class Employees { //父类
    //属性
    //*  分析有一个带薪的月份 13, 15,12
    private String name;
    private double salary;
    private int salMonth = 12; //先默认为12
    //方法


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

    public double getSalary() {
        return salary;
    }

    public String getName() {
        return name;
    }

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

    public int getSalMonth() {
        return salMonth;
    }

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

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

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

2.工人类  Worker.java

package OOPHomework_.Homework_03;

public class Worker extends Employees{ //子类
     //属性
    public Worker(String name, double salary) {
        super(name, salary);
    }

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

3.农民类  Peasant.java

package OOPHomework_.Homework_03;

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

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

4.老师类   Teacher.java

package OOPHomework_.Homework_03;

public class Teacher extends Employees{
       //属性
       private double ClassPay;
       private int days;     //一年上课天数
       //方法

//
//    public Teacher(String name, double salary, double classPay, int days) {
//        super(name, salary);
//        ClassPay = classPay;
//        this.days = days;
//    }


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

    public double getClassPay() {
        return ClassPay;
    }

    public void setClassPay(double classPay) {
        ClassPay = classPay;
    }

    public int getDays() {
        return days;
    }

    public void setDays(int days) {
        this.days = days;
    }

    @Override
    public void printSal() {
        //super.printSal();
        System.out.print("教师 ");
        System.out.println(getName() + " 年工资是 " + ((getSalMonth() * getSalary()) + (days * ClassPay)));

                //(getDays() * getClassPay())));
    }
}

5.科学家类  Scientist.java

package OOPHomework_.Homework_03;

public class Scientist extends Employees{
    //特有属性
    //年终奖 bonus
    private double Bonus;

     //方法

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

//    public Scientist(String name, double salary, double bonus) {
//        super(name, salary);
//        Bonus = bonus;
//    }

    public double getBonus() {
        return Bonus;
    }

    public void setBonus(double bonus) {
        Bonus = bonus;
    }

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

6.服务员类  Waiter.java

package OOPHomework_.Homework_03;

public class Waiter extends Employees{
    public Waiter(String name, double salary) {
        super(name, salary);
    }

    @Override
    public void printSal() {
        System.out.print("服务生 ");
        super.printSal();
    }
}

7.main   Homerwork03_.java

package OOPHomework_.Homework_03;

public class Homerwork03_ {
    public static void main(String[] args) {
        Worker worker = new Worker("梨花",5000);
        worker.setSalMonth(10);//可以改变年工作月份
        worker.printSal();

        Peasant peasant = new Peasant("花花",6000);
        peasant.printSal();

        Teacher teacher = new Teacher("李强",9000);
        teacher.setClassPay(1000);
        teacher.setDays(300);
        teacher.setSalMonth(13);
        teacher.printSal();

        Scientist scientist = new Scientist("呱呱",12000);
        scientist.setBonus(600000);
        scientist.setSalMonth(15);
        scientist.printSal();

        Waiter waiter = new Waiter("小王",8000);
        waiter.printSal();
    }
}

运行截图

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值