-2

我正在做一个练习,它需要 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!

哪个更好,但仍然无法区分狗和猫。

4

2 回答 2

2

基本上你的代码一切都错了。您的任何实现都不会修改您的层级。此外,测试动物的声音可能不是最安全的检查方式。

假设你有一个 Dog 类

class Dog implements Tier {
}

尽管您的描述缺少一些细节,但这可能有效。

public class DogSchool implements PetSchool {
  // make your internal list private to prevent unqualidfied modification
  private ArrayList<Tier> dogs = new ArrayList<>();

  public boolean add(Tier tier) {
    // check the type of the animal and add it to your internal list
    if (tier instanceof Dog) {
      return this.dogs.add(tier);
    }
    return false;
  }

  public boolean addAll(ArrayList<Tier> tiere) {
    // only add the dogs if every animal in the list is a dog
    for (Tier t: tiere) {
      if (!(t instanceof Dog))
        return false;
    }
    return this.dogs.addAll(tiere);
  }

  public boolen remove(Tier tier) {
    return this.dogs.remove(tier);
  }

  public ArrayList<Tier> getTiere() {
    // return a copy so no one can modify your internal list
    return new ArrayList<>(this.dogs);
  }
}
于 2015-12-15T11:11:16.877 回答
1

你有很多错误。需要注意的主要一点是,您应该初始化 tiere List 并在所有方法中使用它,而不是在每个方法中创建新的 ArrayList。

 public ArrayList<Tier> tiere; // you forgot to initialize this ArrayList

 @Override
  public boolean add(Tier t){
      ArrayList<Tier> neu= new ArrayList<Tier>(); 
      if(t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
          return neu.add(t); // this list is local to the method, you should be adding to tiere
          }
      else {
          return false;
      } }


  @ Override 
  public boolean addAll(ArrayList<Tier> tiere){
      ArrayList<Tier> neu= new ArrayList<Tier>(); // remove this list
     return tiere.addAll(neu); // should be this.tiere.addAll(tiere);

  }

  @Override
  public boolean remove(Tier t){
      ArrayList<Tier> neu= new ArrayList<Tier>(tiere.size()); // remove this
      if(!t.gibLaut().equalsIgnoreCase("Wau! Wau!")){
          return neu.remove(t); // should remove from tiere
      }
      else{
          return false;
      }

  }

@Override
public ArrayList<Tier> getTiere() {
    return new ArrayList<Tier>(); // should either return tiere or a copy of it (i.e. new ArrayList<Tier>(tiere))

}
于 2015-12-15T10:59:24.587 回答