Java接口及其实现

时间:2015-12-15 10:51:17

标签: java arrays list interface interface-implementation

我正在做一个练习,需要DogSchool来实施PetSchool。 我打算制作一份在宠物学校注册的动物的数据清单,狗学校需要将狗与其他动物区分开来。狗的特征是他们高喊“Wau!Wau!”。 我纠正了。但它仍然无法区分狗和猫。

Tier = Animal

这是界面代码。

      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!

哪个更好,但它仍然不能区分狗和猫。

2 个答案:

答案 0 :(得分:2)

基本上你的代码都有问题。您的所有实现都不会修改您的平局。另外,测试动物的声音可能不是最安全的检查方式。

假设您有狗类

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);
  }
}

答案 1 :(得分: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))

}