java 将接口存储在数组列表java中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2440596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:08:47  来源:igfitidea点击:

Store interface in array list java

java

提问by user238384

I am learning java. I am trying to use composite design pattern. I am trying to use following logic. ( Don't laugh I know it is very basic :) )

我正在学习java。我正在尝试使用复合设计模式。我正在尝试使用以下逻辑。(别笑,我知道这是非常基本的:))

Item -> interface
Folder -> class
File -> class

In folder class, can I create an arraylist of Item to store files information?

在文件夹类中,我可以创建一个 Item 的数组列表来存储文件信息吗?

ArrayList<Item> info = ArrayList<Item>();

Or should I use Folder Arraylist?

或者我应该使用文件夹 Arraylist 吗?

ArrayList<Folder> info = ArrayList<Folder>();

I don't know if interface can store real data as there is no variable just function definitions.

我不知道接口是否可以存储真实数据,因为没有变量只是函数定义。

Thanks for helping a newbie :)

感谢您帮助新手:)

采纳答案by polygenelubricants

You can do both (with some syntactic correction)

你可以同时做(有一些语法更正)

List<Item> info = new ArrayList<Item>();

With regards to this comment:

关于此评论:

I don't know if interface can store real data as there is no variable just function definitions.

我不知道接口是否可以存储真实数据,因为没有变量只是函数定义。

Interfaces do more than provide function definitions. Most importantly, they define a type. info, declared as above, is a list of objects of type Item. Those objects can most certainly store data.

接口不仅仅提供函数定义。最重要的是,它们定义了一个类型info,如上声明,是一个类型为 的对象列表Item。这些对象肯定可以存储数据。

As an example, consider the following:

例如,请考虑以下情况:

interface Item { ... }
class Folder implements Item { ... }

Item it = new Folder();

Now, itrefers to an instance of Folder, which is an Item.

现在,it指的是 的一个实例Folder,它是一个Item

回答by Michael Koval

As long as Folderand Fileboth implement Item, this will work fine. Be aware, however, that you will only have access to the properties that are defined in the Iteminterface: not those specific to Folderor specific to File.

只要FolderFile都实现Item,这将正常工作。但是请注意,您只能访问Item接口中定义的属性:不能访问特定于Folder或特定于的属性File

From what you've described, it sounds like a reasonable way to approach the problem, although it's hard to say for sure without knowing more about the use of this data structure.

从您所描述的情况来看,这听起来像是一种解决问题的合理方法,尽管在不了解更多有关此数据结构的使用的情况下很难确定。

回答by Carl Manaster

You certainly can create an ArrayList of Items. It is not a problem that Item, as an interface, doesn't have instance variables. The only Items that can be created, however, are not "just" Items - they will be instances of some class that implementsthe Item interface. So, for instance, if Folder and File both implement the Item interface, you can put both Folders and Files into that ArrayList. But the ArrayList would be declared just to hold Items - and, since they implement that interface, both Folders and Files qualify - they areItems.

您当然可以创建一个 ArrayList 的 Items。Item作为接口,没有实例变量,这不是问题。然而,唯一可以创建的项目不仅仅是“项目”——它们将是implementsItem 接口的某个类的实例。因此,例如,如果 Folder 和 File 都实现了 Item 接口,则可以将 Folders 和 Files 都放入该 ArrayList 中。但是 ArrayList 将被声明为仅用于保存项目 - 而且,由于它们实现了该接口,文件夹和文件都符合条件 - 它们项目。

回答by Benjamin Ortuzar

An interface can't store data. An interface is a Type that you can use as a contract that your Files and Folder classes will implement. Since your Files and Folder classes both implement the Item interface, you can add files and folder objects to the List that accepts the Item type.

接口不能存储数据。接口是一种类型,您可以将其用作文件和文件夹类将实现的契约。由于您的 Files 和 Folder 类都实现了 Item 接口,您可以将文件和文件夹对象添加到接受 Item 类型的 List。

List<Item> info = new ArrayList<Item>()