如何根据条件覆盖方法

时间:2017-10-01 23:41:50

标签: java override

在我的BOOKING课程中,我有这种方法来计算总金额

public void calcTotal(){
    total = amount*priceperbooking;

}

我创建了一个名为BOOKING_APPLICATION的子类,如果amount>我想覆盖calcTotal()方法。 5那么它必须减去总数的15%

这是我尝试的但它不起作用:

public void calcTotal(){
 if (super.amount>5) {
     super.total = super.total-(15/100.0*super.total);
 }

}

当我跑它时它给我原来的总数,而不是拿出15% 我做错了什么

编辑:这是我的主要课程的代码

public class Diving_Adventures{
static BOOKING obj1 = new BOOKING();
static BOOKING_APPLICATION obj2 = new BOOKING_APPLICATION();


public static void main(String[] args) {

    obj1.setName();
    obj1.setNumber();
    obj1.setAmount();
    obj1.setPriceperbooking();
    obj1.calcTotal();
    obj2.calcTotal();



    JOptionPane.showMessageDialog(null, obj1.toString());
}


}

我假设通过obj1.calcTotal()它将计算总数,并通过obj2.calcTotal它将取出15%但它不起作用

以下是我的BOOKING课程的完整代码:

private String name;
private int number;
public int amount;
public double priceperbooking;
public double total;





public String getName() {
    return name;
}

public int getNumber() {
    return number;
}

public int getAmount() {
    return amount;
}

public double getPriceperbooking() {
    return priceperbooking;
}

public void setName() {

    this.name =  JOptionPane.showInputDialog("Enter the name");;
}

public void setNumber() {

    this.number = Integer.parseInt(JOptionPane.showInputDialog("Enter the number"));
}

public void setAmount() {

    this.amount =  Integer.parseInt(JOptionPane.showInputDialog("Enter the amount"));
}

public void setPriceperbooking() {

    this.priceperbooking = Double.parseDouble(JOptionPane.showInputDialog("Enter the price per bookin"));;
}


public void calcTotal(){
    total = amount*priceperbooking;

}





@Override
public String toString() {
    return "Customer name: "+name+"\n"+"number"+ number + "\namount=" + amount + 
            "\n priceperbooking=" + priceperbooking + "\n total=" + total ;
}

1 个答案:

答案 0 :(得分:2)

在派生类中:

foo.o

如上所述,你首先要计算~/Documents/Python » python test.py . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK