修复一个类以确保它将对象添加到其列表中,只有它实现了某个接口 - Java

时间:2013-09-15 06:16:42

标签: java interface implementation

我已经获得了一个类 - GoodPetStoreClient - 它利用另一个类 - NoisyPetStore - 来创建一个对象列表 - 猫,狗和牛 - 它们实现了一个接口 - MakesSound - 并且被要求修改NoisyPetStore确保GoodPetStoreClient能够正确编译。我一直在努力找出我所缺少的东西,但到目前为止没有运气,并且会欣赏一些更有经验的见解。

谢谢!

这是代码      公共类GoodPetStoreClient {

    public static void main(String[] args) {
        NoisyPetStore petStore = new NoisyPetStore();
        petStore.addPet(new Cat());
        petStore.addPet(new Cow());
        System.out.println("I bought an animal, and it goes: " + petStore.buyNewestPet().makeNoise()); //moo...
        System.out.println("The rest of the pet store goes: " + petStore.makeHugeNoise()); //meow
        System.out.println("I bought another animal, and it goes: " + petStore.buyNewestPet().makeNoise()); //meow
        petStore.addPet(new Dog());
        System.out.println("The pet store now goes: " + petStore.buyNewestPet().makeNoise());
    }

    private static class CollisionInSpace {
        // makes no sound at all
    }

}

import java.util.ArrayList; import java.util.List;

public class NoisyPetStore 
{

    //Stores pets
    private List list;

    public NoisyPetStore()
    {
        list = new ArrayList();
    }

    /* add a pet to the pet store after checking
    * whether or not the object implements
    * <MakesSound>
    * @param o takes in a object of an unspecified 
    * class
    **/
    public void addPet(Object o )
    {
      //check if the instance implements the MakesSound interface
      if(o instanceof MakesSound)
      {
         list.add(o);
      }
   }

   /* get the last pet from the store by accessing
   * the last item in the list, and hence the one
   * which has been added most recently
   **/
   public Object buyNewestPet()
   {
        Object ans = null;
        if (list.size() > 0)
        {
            ans = list.remove(list.size() - 1);
        }
        return ((MakesSound)ans);
    }

    /* creates a string representation of all of the noises
    * made by pets which have been added to the list using
    * a <StringBuilder>
    * @return returns a String representation of all the noises
    * made by the pets in the list
    **/
    public String makeHugeNoise() {
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            ans.append(((MakesSound) list.get(i)).makeNoise());
        }
        return ans.toString();
    }
}

public class Cat implements MakesSound
{
   String sound;

   /* Constructor for Cat object
   * takes in no parameters and instantiates 
   * the instance variable sound
   **/
   public Cat()
   {
      sound = "Meeow";
   }

   /* Overrides the <makeNoise> method defined by the
   * <MakeSounds> interface
   * takes in no parameters and returns the sound made
   * by a cat, represented as a <String> {@link String}
   **/
   @Override
   public String makeNoise()
   {
       String s = sound;
       return s;
   }
}

//dog and cow have identical codes to cat, with the exception that they produce the sounds "Woof!" and "Moo!" respectively.

1 个答案:

答案 0 :(得分:0)

您可以更改此行:

public void addPet(Object o )

到此:

public void addPet(MakesSound aPet)

而且:

public Object buyNewestPet()

到此:

public MakesSound buyNewestPet()

这将强制您只能将实现MakesSound接口的对象传递给addPet方法。您无需使用instanceof来验证这一点。此外,buyNewestPet()将返回实现MakesSound接口的对象,使其中定义的方法可用于GoodPetstoreClient中的调用。

相关问题