java - 无法访问Parent LinkedList java

标签 java inherited

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

封装registrationui;
导入 java.util.LinkedList;
公共(public)类 CompositeDiscount 实现 IDiscountStrategy{
    LinkedList disList;
    公共(public)综合折扣(){
        disList=新的LinkedList<>();
    }

    @覆盖
    公共(public) int getDiscount(注册 reg) {
       返回0;
    }
    公共(public)无效addDiscount(IDiscountStrategy ids){
        disList.add(ids);
    }
}

这是父类

封装registrationui;
公共(public)类 BestForStudentStrategy 扩展 CompositeDiscount{


     public int getDisocunt(注册reg){
        整数金额=0;
        for(IDiscountStrategy ids:disList){
           if(ids.getDiscount(reg)>金额){
               金额=ids.getDiscount(reg);
           }
        }
        返回金额;
    }
}

这是 child 类

私有(private)无效学术ActionPerformed(java.awt.event.ActionEvent evt){

      if(学术.isSelected())
        rcc.addCompositeDiscount(rcc.getDiscountPloicy("registrationui.AcademicExcellenceDiscount"));
        别的{
            for(IDiscountStrategy ids:rcc.getCompositeDisocunt().disList)
            {
                if(ids.getClass().toString().equals("类(class)注册ui.AcademicExcellenceDiscount"))
                    rcc.getCompositeDisocunt().disList.remove(ids);
            }
        }
    }

通过调用 Resitration 类的 Controller 填充 id 的 UI

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

public IDiscountStrategy getDiscountPolicy(字符串策略){
        尝试 {
            尝试 {
                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);
        }
        返回 ID;
    }

最佳答案

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

关于java - 无法访问Parent LinkedList java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42902213/

相关文章:

java - EBNF 到 JavaCC 词法分析器

java - 按空格而不是包含空格的双引号 (") 拆分 Java String

ruby - ruby 类中的可继承属性

c# - 获取继承类型列表并不适用于所有类型

C# 如何从父列表访问继承的对象

Javamail - 为什么我无法让它工作?

java - Libgdx 有显示列表的实现吗?

java - Subclipse 安装失败 "Subversion native library not available"(Java HL)

c++ - 覆盖 C++ 派生类中的数据成员