简体   繁体   English

不同的 Class,在 List 中具有相同的超类

[英]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!谢谢!

Why isn't this working?为什么这不起作用?

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

Different Approach不同的方法

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.

相关问题 同一Java类的两个版本,每个版本具有不同的超类 - Two versions of the same Java class where each has a different superclass 可以在List上添加不同类型的对象(子类类型)(如果List的类型是它们的公共超类)? - Can different type of objects(sub-class types) added on a List (if type of List is their common superclass)? 序列化后在超类列表中强制转换继承的类 - Casting inherited class in List of Superclass after Serialization 如何获取类及其超类的注释列表 - How to get the list of annotations of a class and its superclass ArrayList包含同一个超类的不同对象 - 如何访问子类的方法 - ArrayList containing different objects of the same superclass - how to access method of a subclass 如何使用相同的SuperClass方法初始化不同的SubClass类型? - how to initialize different SubClass types w/ same SuperClass Method? 同一包和不同包中超类的子类之间的区别是什么? - What is distinction between subclass of superclass in same package and different package? Java-包含一组相同超类的不同子类的数据结构 - Java - data structure to contain a set of different subclasses of the same superclass Groovy类无法在同一包中找到超类 - Groovy class can't locate superclass in the same package 通过超类返回与类名相同的数据类型? - Return data type the same as class name via superclass?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM