多态性:为什么要使用“List list = new ArrayList”?而不是“ArrayList list = new ArrayList”? [英] Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"?

查看:148
本文介绍了多态性:为什么要使用“List list = new ArrayList”?而不是“ArrayList list = new ArrayList”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么要优先选择Java类的接口?


我应该何时使用

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

ArrayList 继承自列表,所以如果 ArrayList 中的某些功能不在列表中,那么我会失去 ArrayList 的一些功能,对吧?尝试访问这些方法时,编译器会发现错误吗?

解决方案

你这样做的主要原因是解耦您的代码来自接口的特定实现。当您编写如下代码时:

  List list = new ArrayList(); 

您的代码的其余部分只知道数据类型为 List ,这是首选,因为它允许您轻松地在 List 界面的不同实现之间切换。



例如,假设您正在编写一个相当大的第三方库,并且说您决定使用 LinkedList 实现库的核心。如果您的图书馆在很大程度上依赖于访问这些列表中的元素,那么最终您会发现您做出了糟糕的设计决策;你会意识到你应该使用 ArrayList (它给出了O(1)访问时间)而不是 LinkedList (给出O(n)访问时间)。假设您已经编程到接口,那么进行这样的更改很容易。您只需更改列表的实例,

 列表列表= new LinkedList(); 

  List list = new ArrayList(); 

您知道这会有效,因为您已编写代码以遵循<提供的合同code>列表界面。



另一方面,如果你使用实现了库的核心LinkedList list = new LinkedList()进行这样的更改并不容易,因为无法保证代码的其余部分不会使用特定于 LinkedList 类的方法。 / p>

总而言之,选择只是设计问题......但这种设计非常重要(特别是在处理大型项目时),因为它允许您可以在不破坏现有代码的情况下进行特定于实现的更改。


Possible Duplicate:
Why should the interface for a Java class be prefered?

When should I use

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

ArrayList inherits from List, so if some features in ArrayList aren't in List, then I will have lost some of the features of ArrayList, right? And the compiler will notice an error when trying to access these methods?

解决方案

The main reason you'd do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

List list = new ArrayList();  

the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you'll find that you've made a poor design decision; you'll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

List list = new LinkedList();

to

List list = new ArrayList();  

and you know that this will work because you have written your code to follow the contract provided by the List interface.

On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn't be as easy, as there is no guarantee that the rest of your code doesn't make use of methods specific to the LinkedList class.

All in all, the choice is simply a matter of design... but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.

这篇关于多态性:为什么要使用“List list = new ArrayList”?而不是“ArrayList list = new ArrayList”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆