简体   繁体   English

Java列表/包含各种动物的动物阵列

[英]Java list/array of animals containing a variety of animals

I need a list of all the animals. 我需要一份所有动物的清单。 I have a basic Animal class and would like to extend it to dogs, cats, iguanas, etc. 我有一个基本的Animal类,并希望将它扩展到狗,猫,鬣蜥等。

Is there a good way to get them all into one List ? 有没有一种方法可以将它们全部放入一个List

I want to be able to check the entire list of all animals without having to change all the previously made animals. 我希望能够检查所有动物的完整列表,而无需更改所有以前制作的动物。 They should know about all the other Animal classes made after them. 他们应该知道他们之后制作的所有其他Animal类。

You can make any number of Animal s by extending an Animal base class like this: 您可以通过扩展Animal基类来制作任意数量的Animal

public class Dog extends Animal {

}

Then you can declare a new list of animals. 然后你可以宣布一份新的动物名单。

List<Animal> animals = new ArrayList<Animal>();

Once you have done that, you can add any number of new objects the extend the Animal class to your list. 完成后,您可以添加任意数量的新对象,将Animal类扩展到列表中。

animals.add(new Dog());
animals.add(new Cat());
animals.add(new Iguana());

You can create a List<Animal> like this: 您可以像这样创建List<Animal>

// create empty list
List<Animal> animals = new ArrayList<>();

// add any kind of animal
animals.add(new Kangaroo());
animals.add(new Butterfly());

// use that list later
int numberOfAnimals = animals.size();
for (Animal animal : animals) {
    // do something
}

Things to keep in mind: 要记住的事情:

  • when using items of the animal list you do not know anymore which kind of animal it is. 当使用动物清单中的物品时,你不再知道它是哪种动物。 If you want to access Kangaroo-specific details of an animal you should check, whether the item actually is a Kangaroo (using instanceof ) 如果您想要查看动物的袋鼠特定细节,您应该检查该物品是否真的袋鼠(使用instanceof
  • You can only add objects to the list of animals, whose class extends Animal . 您只能将对象添加到动物列表中,其类extends Animal
  • In general it is a good idea to keep your code as general as posible . 一般来说,保持代码尽可能通用是一个好主意。 If you need special features of an ArrayList , then make your animals an ArrayList . 如果您需要ArrayList特殊功能,那么请将您的动物ArrayList But in most cases defining the variable as List or even Collection (or Iterable ) makes your code more flexible. 但在大多数情况下,将变量定义为List或甚至Collection (或Iterable )会使您的代码更加灵活。

It is called Polymorphism . 它被称为多态性 So basically, you answered to your question by yourself. 所以基本上,你自己回答了你的问题。 It is perfectly legal in Java to add to collection objects of subtypes of declared collection's type. 在Java中添加已声明集合类型的子类型的集合对象是完全合法的。

You can use List<? extends Animal> animals = new ArrayList<>(); 你可以使用List<? extends Animal> animals = new ArrayList<>(); List<? extends Animal> animals = new ArrayList<>(); and then add any type of animal to the list. 然后将任何类型的动物添加到列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何返回列表中动物的名称<Animals>使用列表<Caretaker>指定看守者 java8 - how can i return the name of animals that are in a List<Animals> using a List<Caretaker> specifying a caretaker java8 扩展动物的猎豹类。 爪哇 - Cheetah class that extends animals. Java 公众有什么区别 <T extends Animal> void addAll(List <T> 动物)和public void addAll(List <Animal> 动物)? - What is the difference between public <T extends Animal> void addAll(List<T> animals) and public void addAll(List<Animal> animals)? 打印低于阈值的动物总数 - Printing the sum of animals that are below a threshold 返回一个按字母顺序排序的动物列表,其最后一个字母不以参数中列出的任何字母结尾 - returning a an alphabetically sorted list of animals whose last letter does not end any letters listed in the argument 如何使用收集对象打印所有动物 - How to print all Animals using Collection Object 如何用名字显示所选动物的描述? - How to display the description of the selected animals with names? 为什么我不能创建一个类? 我正在尝试使用 Animal 数组来容纳不同的动物 - Any reason why I can't create a class? I'm trying to have a Animal array to hold different animals 在狐狸和兔子的同一场中有多只动物模拟 - Having multiple animals in the same field in foxes and rabbits simulation 如何让动物在我的虚拟动物园中以随机的机会成功繁殖? - How do I allow for animals to breed, with a random chance of success, in my virtual zoo?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM