Ich habe Elternklasse CompositeDiscount. Es hat eine verknüpfte Liste, um einige Dicounts zu speichern. Es hat zwei Kind-Klasse BestForVersityStrategy und BestForStudentStrategy. Ich bevölker die Eltern verknüpfte Liste in einer anderen Klasse Registrierung. Wenn ich versuche, LL von der Benutzeroberfläche zu iterieren, gibt es die Rabattliste, aber wenn ich sie von einem Kind aus anrufe (d. H. BestForVersity), zeigt es an, dass die Liste leer ist.Zugriff auf übergeordnete LinkedList-Java nicht möglich
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);
}
}
dies Elternklasse
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;
}
}
ist dieses Kind Klasse
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, von wo es id über die Steuerung bevölkert, die Resitration Klasse
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;
}
public void addCompositeDiscount(IDiscountStrategy ids){
cds.addDiscount(ids);
bfn=new BestForNsuStrategy();
bfs=new BestForStudentStrategy();
}
Wie beantwortet unten erklärt ist: http://stackoverflow.com/questions/9898097/ Was-sind-die-Regeln-diktieren-die-Vererbung-von-statischen-Variablen-in-Java, wie unten beantwortet. –