[英]Different Class, with same superclass in a List
Is it possible to put classes with the same superclass in an ArrayList and that I can retrieve the fields of the subclass?是否可以将具有相同超类的类放在 ArrayList 中,并且我可以检索子类的字段?
public class Animal {
private String name;
public String getName() { ... }
}
public class Dog extends Animal {
private String tailLength;
public String getTailLength() { ... }
}
public class Bird extends Animal {
private String beakSize;
public String getBeakSize() { ... }
}
Now I'd like to put them in an arrayList现在我想把它们放在 arrayList
private List<Animal> animals = new ArrayList<>();
animals.put(new Dog());
animals.put(new Bird());
I'm able successfully put them in the list, but when I retrieve the value from the list, I'm not able to get the beakSize from the bird class and the tailLength from the dog class.我能够成功地将它们放入列表中,但是当我从列表中检索值时,我无法从鸟 class 和狗 class 中获取 beakSize。
Can you suggest of a strategy how to do this?你能建议一个如何做到这一点的策略吗? Thanks!
谢谢!
You've got list of type Animal
.你有
Animal
类型的列表。 This means that the compiler has one and only one guarantee.这意味着编译器只有一个保证。 That this object has the fields and methods associated with the
Animal
superclass.这个 object 具有与
Animal
超类关联的字段和方法。 Test this out for yourself.自己测试一下。
Animal myBird = new Bird();
myBird.getBeakSize(); // This won't compile.
The compiler doesn't know it's of type Bird
because its reference type is Animal
.编译器不知道它是
Bird
类型,因为它的引用类型是Animal
。
What you need to do is rethink your class design, such that your superclass has the necessary information for you to access.您需要做的是重新考虑您的 class 设计,以便您的超类有必要的信息供您访问。
If you need access to the specific subclass, then I would recommend keeping separate lists of each subclass, possibly inside of an object.如果您需要访问特定的子类,那么我建议保留每个子类的单独列表,可能在 object 内。
An alternative might be something like this:另一种可能是这样的:
public abstract class Animal {
private String name;
public String getName() { ... }
public abstract String getFaceDepth(); //
}
public class Bird {
public String getFaceDepth() {
// Return beak size here.
}
}
Now, you'll notice here that face depth isn't particularly helpful.现在,您会注意到这里的面部深度并不是特别有用。 This is the problem of making general cases.
这就是制作一般情况的问题。 Sometimes, they're so general, they lose the essence of the data they're representing.
有时,它们太笼统了,以至于失去了它们所代表的数据的本质。 This can be a sign that your class structure needs to be thought about, depending on your problem.
这可能表明您的 class 结构需要考虑,具体取决于您的问题。
When you are retrieving elements from array, you can use 'instanceof' method:从数组中检索元素时,可以使用“instanceof”方法:
Animal animal = // get element/iterate over array
if(animal instanceof Dog) {
Dog dog = (Dog) animal;
// continue here...
}
if(animal instanceof Bird) {
Bird bird = (Bird) animal;
// continue here...
}
You need to have 2 interfaces你需要有2个接口
public interface Beak {
String getBeakSize();
}
public interface Tail {
String getTailSize();
}
Now your code would look like this现在你的代码看起来像这样
public class Animal {
private String name;
public String getName() { ... }
}
public class Dog extends Animal implements Tail {
private String tailLength;
@Override
public String getTailLength() { ... }
}
public class Bird extends Animal {
private String beakSize;
@Override
public String getBeakSize() { ... }
}
You can add items as you were doing before.您可以像以前一样添加项目。
private List<Animal> animals = new ArrayList<>();
animals.put(new Dog());
animals.put(new Bird());
But now, you can use the interfaces to find if the animal has tail or beak or both or nothing at all.但是现在,您可以使用界面来查找动物是否有尾巴或喙,或两者兼有,或根本没有。
for(Animal animal in animals) {
if(animal instanceof Tail) {
System.out.println(((Tail)animal).getTailLength());
}
if(animal instanceof Beak) {
System.out.println(((Beak)animal).getBeakLength());
}
}
Why this is important?为什么这很重要? This way you can have another animal lets say Cat or another bird which also have tail.
这样你就可以拥有另一种动物,比如说猫或另一种也有尾巴的鸟。 This piece will work with all of them.
这件作品适用于所有人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.