我有父类CompositeDiscount。它有一个链接列表来存储一些dicount。 它有两个子类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;
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;
}
}
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;
}
}
通过调用Resitration class
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);
}
}
}
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);
}
}
}
public void addCompositeDiscount(IDiscountStrategy ids){
cds.addDiscount(ids);
bfn=new BestForNsuStrategy();
bfs=new BestForStudentStrategy();
}
public void addCompositeDiscount(IDiscountStrategy ids){
cds.addDiscount(ids);
bfn=new BestForNsuStrategy();
bfs=new BestForStudentStrategy();
}
答案 0 :(得分:1)
CompositeDiscount中的链接列表是静态的。 Java中的静态变量不是继承的,它们只存在于声明它们的类中
答案 1 :(得分:0)
public static LinkedList<IDiscountStrategy> disList;
您必须使用静态
的公共修饰符