Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

Inheritance in Java subclass

I'm trying to write a class(SalaryEmployee) and subclass(ComissionEmployee) but I'm not getting the right salary in the main and I could not do a se1.setSalesAmount(20000). Where did I go wrong?

This is what is in main:

public static void main(String[] args) {
    SalaryEmployee se1 = new CommissionEmployee("E3", "John", 10000);  
    se1.setSalesAmount(20000) // not found
    System.out.println(se1.getSalary());

}

SalaryEmployee which has id (String), name(String), salary(double). It has a constructor to initialise the instance variables. It has a getSalary method that returns the salary as well as a toString method that returns the values of the instance variables.

public class SalaryEmployee  {
    private String id;
    private String name;
    double salary;

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

    public double getSalary() {
        return salary;
    }

    public String toString() {
        return "ID=" + id + " Name=" + name + " Salary=" + salary;
    }
}

Then in ComissionEmployee I need to write it as a subclass of SalaryEmployee which has id(String), name(String), baseSalary(double), salesAmount(double). It has a constructor to initialise id, name and baseSalary only. It has get/set methods for salesAmount. It has a getSalary method that returns the salary based on the baseSalary + commission as follows:

salesAmount below 10,000 - No commission, at least 10,000 but below 20,000 - 3% of baseSalary, at least 20,000 - 5% of baseSalary

public class CommissionEmployee extends SalaryEmployee {

    private double baseSalary;
    private double salesAmount;

    public CommissionEmployee(String id, String name, double salary) {
        super(id, name, salary);
        this.baseSalary = salary;

    }

    public void setSalesAmount(double amount) {
        this.salesAmount = amount;
    }

    public double getSalesAmount() {
        return salesAmount;
    }

    public double getSalary() {
        if (salesAmount <= 10000 || salesAmount < 20000) {
            return 1.03 * baseSalary;
        } else if (salesAmount > 20000) {
            return 1.05 * baseSalary;
        } else if (salesAmount < 10000) {
            return baseSalary;
        }
        return baseSalary;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You created se1 as SalaryEmployee type which doesn't have setSalesAmount() method. So,you need to downcast your se1 to CommissionEmployee:

((CommissionEmployee)se1).setSalesAmount(20000)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...