继承Java super.print()不返回值

时间:2016-10-05 20:12:56

标签: java inheritance methods casting

我的代码不会打印佣金的付费,当前时间和总销售额,我认为这可能与继承有关,但我不确定。我把它们放在另一个类中,但我认为问题出在这里是因为我的其他类的转换没有问题。除了委员会的3个值之外,所有值都返回到程序中。每小时的薪资和当前时间更新哪个委员会是继承的,但它在委员会班级中不起作用。

//StaffMember Class
public abstract class StaffMember {
protected String name, address, number;

public StaffMember(String name, String address, String number) {
    this.name = name;
    this.address = address;
    this.number = number;
}

public String toString(){
    return "Name: " +name + "\nAddress: " +address + "\nPhone: " +number;
}

public abstract double pay();
}

//Employee Class
public class Employee extends StaffMember{
    private String rsiNumber;
    protected double payRate;

public Employee(String rsiNumber, double payRate, String name, String address, String number) {
    super(name, address, number);
    this.rsiNumber = rsiNumber;
    this.payRate = payRate;
}

public String toString(){
    return super.toString() + "\nRSI Number: " +rsiNumber + String.format("\nPaid: €%,.2f" ,pay());
}

public double pay(){
    return payRate;
}
}


//Hourly Class
public class Hourly extends Employee{
    private int hours;

public Hourly(String rsiNumber, double payRate, String name, String address, String number) {
    super(rsiNumber, payRate, name, address, number);
    this.hours = 0;
}

public void addHours(int hours){
    this.hours=hours;
}

public double pay(){
    int otHours=0;
    if(hours>40){
        otHours=hours-40;
    }
    double pay=(hours-otHours)*payRate+(otHours*payRate*1.5);
    return pay;
}

public String toString(){
    return super.toString() + "\nCurrent Hours: " +hours;
}
}


//Commission Class
public class Commission extends Hourly{
    private double totalSales, commissionRate;

public Commission(double commissionrate, String rsiNumber, double payRate, String name, String address, String number) {
    super(rsiNumber, payRate, name, address, number);
    this.commissionRate = commissionrate;
    this.totalSales=0;
}

public void addSales(int sales){
    this.totalSales=sales;
}

public double pay(){
    return super.pay()+(totalSales*commissionRate);
}

public String toString(){
    return super.toString() + "\nTotal Sales: " +totalSales;
}
}


//Casting Method From Another Class
public void processStaff(){
    for(int i=0; i<sm.length; i++){
        if (sm[i] instanceof Hourly) {
            Hourly h = (Hourly) sm[i];
            if(h.name.equals("Joanne")){
                h.addHours(50);
            }
        }else if (sm[i] instanceof Executive) {
            Executive e = (Executive) sm[i];
            if(e.name.equals("John")){
                e.awardBonus(500);
            }
        }else if (sm[i] instanceof Commission) {
            Commission c = (Commission) sm[i];
            if(c.name.equals("Molly")){
                c.addHours(55);
                c.addSales(400);
            }else if(c.name.equals("Joe")){
                c.addHours(45);
                c.addSales(950);
            }
        }
    }
}

这是我得到的值得到的输出,而不是0。

编辑:似乎已经找到了解决办法,只需将委员会纳入每小时,因为委员会是一种每小时。

public void processStaff() {
    for (int i = 0; i < sm.length; i++) {
        if (sm[i] instanceof Hourly) {
            Hourly h = (Hourly) sm[i];
            if (h.name.equals("Joanne")) {
                h.addHours(50);
            } else if (sm[i] instanceof Commission) {
                Commission c = (Commission) sm[i];
                if (c.name.equals("Molly")) {
                    c.addHours(55);
                    c.addSales(400);
                } else if (c.name.equals("Joe")) {
                    c.addHours(45);
                    c.addSales(950);
                }
            }
        } else if (sm[i] instanceof Executive) {
            Executive e = (Executive) sm[i];
            if (e.name.equals("John")) {
                e.awardBonus(500);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

       if (sm[i] instanceof Hourly) {

       /* .... */
       }else if (sm[i] instanceof Commission) {
            Commission c = (Commission) sm[i];
            if(c.name.equals("Molly")){
                c.addHours(55);
                c.addSales(400);
            }else if(c.name.equals("Joe")){
                c.addHours(45);
                c.addSales(950);
            }
       }

第一个条件适用于Commission,因此您永远不会在其上调用addHours或addSales。因此无需展示。

您真的应该使用调试器来查看类的内容。