0

我有父类 CompositeDiscount。它有一个链表来存储一些折扣。它有两个子类 BestForVersityStrategy 和 BestForStudentStrategy。我在另一个类注册中填充父链表。当我尝试从 UI 迭代 LL 时,它会给出折扣列表,但是当我从子级(即 BestForVersity)调用它时,它显示列表为空。

package registrationui;
import java.util.LinkedList;
public class CompositeDiscount implements IDiscountStrategy{
    LinkedList<IDiscountStrategy> disList;
    public CompositeDiscount(){
        disList= new LinkedList<>();
    }

    @Override
    public int getDiscount(Registration reg) {
       return 0;
    }
    public void addDiscount(IDiscountStrategy ids){
        disList.add(ids);
    }
}

这是父类

package registrationui;
public class BestForStudentStrategy extends CompositeDiscount{


     public int getDisocunt(Registration reg){
        int amount=0;
        for(IDiscountStrategy ids:disList){
           if(ids.getDiscount(reg)>amount){
               amount=ids.getDiscount(reg);
           }
        }
        return amount;
    } 
}

这是儿童班

private void academicActionPerformed(java.awt.event.ActionEvent evt) {                                         

      if(academic.isSelected())
        rcc.addCompositeDiscount(rcc.getDiscountPloicy("registrationui.AcademicExcellenceDiscount"));
        else{
            for(IDiscountStrategy ids:rcc.getCompositeDisocunt().disList)
            {
                if(ids.getClass().toString().equals("class registrationui.AcademicExcellenceDiscount"))
                    rcc.getCompositeDisocunt().disList.remove(ids);
            }
        }
    }     

UI 从哪里通过调用 Resitration 类的控制器填充

public void addCompositeDiscount(IDiscountStrategy ids){        
       cds.addDiscount(ids);
       bfn=new BestForNsuStrategy();      
       bfs=new BestForStudentStrategy();  
    }

public IDiscountStrategy getDiscountPolicy(String policy){
        try {
            try {
                ids=(IDiscountStrategy)Class.forName(policy).newInstance();
            } catch (InstantiationException ex) {
                Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ids;
    }

4

2 回答 2

1

CompositeDiscount 中的链表是静态的。Java中的静态变量不是继承的,它们只存在于声明它们的类中

于 2017-03-20T11:26:53.753 回答
0
public static LinkedList<IDiscountStrategy> disList;

您必须使用 public 修饰符进行静态

于 2017-03-20T11:35:35.397 回答