如何在修改此属性的子类中保留超类属性的跟踪?

时间:2017-03-26 21:33:48

标签: java inheritance

我有两个类Order和ValidatedOrder。 ValidatedOrder是订单的子类,我们在其中指定验证订单的助手的ID以及他可能对最终价格应用的减少量。但是我想要跟踪将从超级类继承的旧价格。 这是订单类:

public class Commande {
private int idCommande;
private int idClient ;
private String Commentaire; 
private ArrayList<LigneCommande> Liste ;

public double calculMontantCommande(){
    int i ;
    for (i=0; i<this.getListe().size();i++){
        montant= montant + (Liste.get(i).getMontantCommande() * Liste.get(i).getQuantiteCommande());
    }

    return montant ;

}

这是ValidatedOrder类:

public class CommandeValidee extends Commande {
private int Assistant ; 
private Double reduction ;


@Override
public double calculMontantCommande() {
    // TODO Auto-generated method stub
    return (super.calculMontantCommande() * reduction);

}

如何解决这个问题的任何线索都会很棒! 感谢

1 个答案:

答案 0 :(得分:2)

super.calculMontantCommande()

返回“旧价格”(未减价的价格)。

因此,在CommandeValidee课程中,您可以添加类似

的方法
public double getPriceWithoutReduction() {
    return super.calculMontantCommande();

}

然后你可以通过调用这个方法来获得它

CommandeValidee order =  ... //Load somehow
double priceWithoutReduction = order.getPriceWithoutReduction();

无论如何,我建议你在开发应用程序时使用英语。那是因为您将使用的大多数库和代码(语言本身)都是用英语编写的。如果类和方法名称不是用英语写的,那么像我们这样的人也会帮助你。想象一下,如果它是用中文写的:D