4题答案,请老师点评,谢谢

来源:11-14 测评作业

慕码人5147775

2025-02-22

三个类:HeadOffice、BranchOffice、OfficeTest
HeadOffice类代码:

package com.imooc.test;

public class HeadOffice {
    private int monthlySalary; // 月工资
    private String performanceRating; //绩效等级

    public HeadOffice() {
    }

    public HeadOffice(int monthlySalary, String performanceRating) {
        this.monthlySalary = monthlySalary;
        this.performanceRating = performanceRating;
    }

    public int getMonthlySalary() {
        return monthlySalary;
    }

    public void setMonthlySalary(int monthlySalary) {
        this.monthlySalary = monthlySalary;
    }

    public String getPerformanceRating() {
        return performanceRating;
    }

    public void setPerformanceRating(String performanceRating) {
        this.performanceRating = performanceRating;
    }

    final public void calculationBonus() {
        /*
         * 绩效为 A:奖金 = 月工资 * 90% * 3
           绩效为 B:奖金 = 月工资 * 90% * 2
           绩效为 C:奖金 = 月工资 * 90%
           绩效为 D:奖金 = 月工资 * 90% * 0.5
           绩效为 E:没有奖金
         */
        float bonus = 0; //奖金
        switch (performanceRating) {
            case "A":
                bonus = (float) (monthlySalary * 0.9 * 3);
                break;
            case "B":
                bonus = (float) (monthlySalary * 0.9 * 2);
                break;
            case "C":
                bonus = (float) (monthlySalary * 0.9);
                break;
            case "D":
                bonus = (float) (monthlySalary * 0.9 * 0.5);
                break;
            case "E":
                bonus = 0;
                break;
            default:
                System.out.println("绩效等级输入错误");
        }
        System.out.println(",绩效等级:" + performanceRating + ",奖金:" + bonus + "元");
    }
}

BranchOffice类代码:

package com.imooc.test;

public class BranchOffice extends HeadOffice{
    private String name; //

    public BranchOffice(int monthlySalary, String performanceRating, String name) {
        super(monthlySalary, performanceRating);
        this.name = name;
    }

    public BranchOffice() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void name () {
        System.out.print(getName());
    }
}

OfficeTest类代码:

package com.imooc.test;

public class OfficeTest {
    public static void main(String[] args) {
        HeadOffice ho1 = new BranchOffice(8000, "C", "小慕");
        BranchOffice bo1 = (BranchOffice) ho1;
        bo1.name();// 小慕
        ho1.calculationBonus();
        HeadOffice ho2 = new BranchOffice(9000, "A", "大毛");
        BranchOffice bo2 = (BranchOffice) ho2;
        bo2.name(); // 大毛
        ho2.calculationBonus();
    }
}

写回答

1回答

彭彭老师

2025-02-23

final 关键字修饰的方法,不能被重写,但可以被继承,子类对象可以直接调用父类的 calculationBonus 方法,测试类可以简化一下

https://img1.sycdn.imooc.com/szimg/53327c6709ba97b211720846.jpg

0
1
慕码人5147775
对的,可以继承,谢谢老师。
2025-02-23
共1条回复

Java零基础入门与实战

Java零基础入门与实战 多场景|多案例|全栈项目

277 学习 · 220 问题

查看课程