我正在做一个练习,它需要 DogSchool 来实现 PetSchool。我打算做一个在宠物学校注册的动物的数组列表,狗学校需要将狗与其他动物区分开来。狗的特征是他们大喊“哇!哇!”。我已更正。但它仍然无法区分狗和猫。
等级 = 动物
这是接口的代码。
import java.util.ArrayList;
public interface PetSchool {
boolean add(Tier tier);
boolean addAll(ArrayList<Tier> tiere);
boolean remove(Tier tier);
ArrayList<Tier> getTiere();
}
This is the code of Implementation.
Please tell me what's wrong with it.
import java.util.ArrayList;
public class DogSchool implements PetSchool {
public ArrayList<Tier> tiere= new ArrayList<Tier>();
@Override
public boolean add(Tier t){
if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.add(t);
}
else {
return false;
} }
@ Override
public boolean addAll(ArrayList<Tier> tiere){
return this.tiere.addAll(tiere);
}
@Override
public boolean remove(Tier t){
if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
return tiere.remove(t);
}
else{
return false;
}
}
@Override
public ArrayList<Tier> getTiere() {
return new ArrayList<Tier>(this.tiere);
}
}
好吧,问题发生在demoTest中:
import java.util.ArrayList;
public class TierDemo {
public static void main(String[] args) {
System.out.println("Test Teil 2:");
DogSchool schule = new DogSchool();
schule.add(new Hund());
schule.add(new Katze());
schule.add(new Hund());
schule.add(new Katze());
schule.addAll(tiere);
for (Tier t : schule.getTiere()) {
System.out.println(t.gibLaut());
}
}
编译后显示:
Test Teil 2:
Wau! Wau!
Wau! Wau!
Miau!
Wau! Wau!
哪个更好,但仍然无法区分狗和猫。