Java 原因 - List list = new ArrayList();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18329311/
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-08-12 00:33:34  来源:igfitidea点击:

Reason for - List list = new ArrayList();

javalistarraylist

提问by codepleb

I've seen code like this many times:

我已经多次看到这样的代码:

List<String> list = new ArrayList<String>();

List<String> list = new ArrayList<String>();

Why do people take the parent of ArrayList(and other classes) instead of the type of the generated object?

为什么人们采用ArrayList(和其他类)的父类而不是生成对象的类型?

Does that take less performance? Or why should someone do this?

这会降低性能吗?或者为什么有人要这样做?

采纳答案by MD Sayem Ahmed

When someone writes code like this, he/she is trying to follow a basic OO design principle which says -

当有人编写这样的代码时,他/她试图遵循基本的面向对象设计原则,即 -

Program to an interface, not to a concrete implementation

编程到接口,而不是具体的实现

I have explained this principle in one of my blog posts. Look in the Class Inheritance VS Interface Inheritancesection.

我在我的一篇博文中解释了这个原则。看Class Inheritance VS Interface Inheritance栏目。

To summarize the post, when you use a reference of a parent type to refer to an instance of a sub-type, you get a lot of flexibility. For example, if you ever need to change your sub-type implementation in the future, you will be able to do that easily, without changing much of your code.

总结这篇文章,当您使用父类型的引用来引用子类型的实例时,您将获得很大的灵活性。例如,如果您将来需要更改您的子类型实现,您将能够轻松地做到这一点,而无需更改您的大部分代码。

Consider the following method -

考虑以下方法 -

public void DoSomeStuff(Super s) {
    s.someMethod();
}

and a call to this method -

并调用此方法 -

DoSomeStuff(new Sub());

now, if you ever need to change the logic inside someMethod, you can easily do it by declaring a new subtype of Super, say NewSubType, and changing the logic inside that implementation. In this way, you will never have to touch other existing code which utilizes that method. You will still be able to use your DoSomeStuffmethod in the following way -

现在,如果您需要更改 内部的逻辑someMethod,您可以通过声明一个新的子类型Super,例如NewSubType,并更改该实现中的逻辑来轻松完成。通过这种方式,您将永远不必接触使用该方法的其他现有代码。您仍然可以DoSomeStuff通过以下方式使用您的方法 -

DoSomeStuff(new NewSubType());

Had you declared the parameter of DoSomeStuffto be of Sub, you would then have to change its implementation too -

如果您将参数声明DoSomeStuff为 of Sub,那么您也必须更改其实现 -

DoSomeStuff(NewSubType s) {
    s.someMethod();
}

and it may also chain/bubble to several other places.

它也可能链接/冒泡到其他几个地方。

In terms of your collection example, this lets you change the list implementation that a variable is pointing to without much hassle. You can easily use a LinkedListin place of an ArrayList.

就您的集合示例而言,这使您可以轻松更改变量指向的列表实现。您可以轻松地使用 aLinkedList代替ArrayList

回答by Ruchira Gayan Ranaweera

List<String> list = new ArrayList<String>();

In collection framework Listis an interface while ArrayListis implementation. Main reason you'd do this is to decouple your code from a specific implementationof the interface also this will be helpful in case if you wish to move to some other implementation of Listin the future.

集合框架List是一个接口,ArrayList而是实现。您这样做的主要原因是将您的代码与特定implementation的接口分离,如果您希望List将来转移到其他一些实现,这也将很有帮助。

回答by christopher

It means you can swap out the type of listat any point with anything that implements the Listinterface, as opposed to creating a rigid model that can only use ArrayList. For example:

这意味着您可以list在任何时候用实现List接口的任何东西替换 的类型,而不是创建一个只能使用ArrayList. 例如:

private List<String> list;

public SomeConstructor()
{
     // At this point, you can make it any type of object you want.
     list = new ArrayList<String>();
     list = new LinkedList<String>();
     list = new AttributeList<String>();
}

This will abstractyour code that uses the listobject, away from the details like what exact object type listis. All it needs to know is that it has the addmethod etc. This is called Loose Coupling.

这将使abstract您使用该list对象的代码远离详细信息,例如确切的对象类型list是什么。它只需要知道它有add方法等。这称为松耦合

回答by Philipp Sander

Because a method doesn't have to know what list-implementation you use.

因为方法不必知道您使用什么列表实现。

A Method just needs to know that is is a list.

方法只需要知道这是一个列表。

The Method can still be used.

该方法仍然可以使用。

Always program to an interface, not to a concrete implementation. (In this case List)

始终针对接口进行编程,而不是针对具体实现进行编程。(在这种情况下列表)

回答by Adrian Pronk

Generally it is preferred to work with the Interface class (Listin this case) so that any List implementation could later be substituted with minimal fuss if requirements change.

通常,最好使用 Interface 类(List在这种情况下),以便以后在需求发生变化时可以用最少的麻烦替换任何 List 实现。

Although ArrayListpossibly supports some methods that are not on the Listinterface, this declaration makes it clear that those extra methods are not relevant in that case.

虽然ArrayList可能支持一些不在List接口上的方法,但这个声明清楚地表明那些额外的方法在这种情况下是不相关的。

回答by Maroun

When you write:

当你写:

List<String> list = new ArrayList<String>();

Then you are sure you'll use only the functionality of the interfaceList.
(ArrayListimplements List, so Listis more flexibl). Using this, allows you to change the ArrayListto other types in the future (like LinkedList..).

然后您确定您将只使用界面的功能List
ArrayList实现List,所以List更灵活)。使用它,您可以ArrayList在将来将 更改为其他类型(例如LinkedList..)。

回答by Maxim Shoustin

To sort things out:

整理一下:

For more flexibility you initiate interface List:

为了获得更大的灵活性,您可以启动界面List

So if you don't need all ArrayListuse Listonly.

因此,如果您不需要全部ArrayList使用List

You can write something like: List<String> = Arrays.asList("aa", "bb","cc").

你可以写类似:List<String> = Arrays.asList("aa", "bb","cc")

For sure, less functionality can help to performance. As you know If you want to use multithreaded application, use Vectorinstead but it will down your performance.

当然,较少的功能有助于提高性能。如您所知,如果您想使用多线程应用程序,请Vector改用,但它会降低您的性能。

enter image description here

在此处输入图片说明

Took from here

这里