[英]How to convince a Java Constructor to accept a List containing objects whose Class implements a certain Interface
[英]Fixing a class to ensure that it adds objects to its list, only if it implements a certain interface - Java
我得到了一个类-GoodPetStoreClient-它使用另一个类-NoisyPetStore-以创建对象列表-Cat,Dog和Cow-实现一个接口-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
}
}
导入java.util.ArrayList; 导入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.
您可以更改此行:
public void addPet(Object o )
对此:
public void addPet(MakesSound aPet)
和这个:
public Object buyNewestPet()
对此:
public MakesSound buyNewestPet()
这将强制您只能将实现MakesSound
接口的对象传递给addPet
方法。 您将不需要使用instanceof
来验证这一点。 buyNewestPet()
还将返回实现MakesSound
接口的对象,从而使在那里定义的方法可用于在GoodPetstoreClient
调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.