Hallo zusammen,
ich habe eine Frage zum this.Operator.
Beim Erstellen einer for each Schleife gab es folgende zwei Fälle:
Fall 1:
public boolean wareImEinkaufswagenEnthalten(String name){
for( Ware ware : this.inhalt ){
if(ware.getName().equals(name))
return true;
}
return false;
}
Fall 2:
public HashMap<String, Integer> bestimmeAnzahlGesuchterWaren(List<String>warenNamen){
HashMap<String, Integer> counts = new HashMap<String,Integer>();
for(String name : warenNamen){
for (Ware ware : inhalt){
if (ware.getName().equals(name)){
if (counts.containsKey(name)) {
Integer anzahl = counts.get(name);
counts.put(name, anzahl +1);
} else {
counts.put(name, 1);
}
}
}
}
return counts;
}
Meine Frage ist nun, warum bei inhalt einmal this und einmal nicht this steht. Im ersten Fall war inhalt ein Attribut der Klasse in der die Methode war. Im zweiten Fall wurde das Attribut inhalt "nur" geerbt.
Warum mache ich also einen Unterschied mit this?
Danke schonmal im voraus.
ich habe eine Frage zum this.Operator.
Beim Erstellen einer for each Schleife gab es folgende zwei Fälle:
Fall 1:
public boolean wareImEinkaufswagenEnthalten(String name){
for( Ware ware : this.inhalt ){
if(ware.getName().equals(name))
return true;
}
return false;
}
Fall 2:
public HashMap<String, Integer> bestimmeAnzahlGesuchterWaren(List<String>warenNamen){
HashMap<String, Integer> counts = new HashMap<String,Integer>();
for(String name : warenNamen){
for (Ware ware : inhalt){
if (ware.getName().equals(name)){
if (counts.containsKey(name)) {
Integer anzahl = counts.get(name);
counts.put(name, anzahl +1);
} else {
counts.put(name, 1);
}
}
}
}
return counts;
}
Meine Frage ist nun, warum bei inhalt einmal this und einmal nicht this steht. Im ersten Fall war inhalt ein Attribut der Klasse in der die Methode war. Im zweiten Fall wurde das Attribut inhalt "nur" geerbt.
Warum mache ich also einen Unterschied mit this?
Danke schonmal im voraus.