英文:
Difference between List<String> strings = new LinkedList<>(); and LinkedList<String> strings = new LinkedList<>();
问题
我正在学习Java中的接口,其中一个示例是可以使用List接口创建一个链表,代码如下:List<String> strings = new LinkedList<>();但是他们没有详细解释。与使用LinkedList<String> strings = new LinkedList<>();相比,我为什么要这样做呢?
英文:
I am learning about interfaces in Java and one example they give is being able to create a linked list using the List interface List<String> strings = new LinkedList<>();
but they don't go into any detail. Why would I want to do this as oppose to LinkedList<String> strings = new LinkedList<>();
?
答案1
得分: 3
List<T>
是一个接口,这意味着在你扩展 List
时必须实现一定数量的方法。LinkedList
恰好扩展了 List
。当你执行 List<String> strings = new LinkedList<>();
时,Java 只看到了 List
的方法。然而,当你执行 LinkedList<String> strings = new LinkedList<>();
时,Java 将其视为 LinkedList
,并且当你调用 getLast
等 LinkedList
具有的方法时,它会正常工作。人们主要之所以执行 List<String> strings = new LinkedList<>();
是为了可读性,但实际效果应该是差不多的。
英文:
List<T>
is an interface, meaning there are a certain number of methods that you must implement when you extend List
. LinkedList
happens to extend List
. When you do List<String> strings = new LinkedList<>();
, java only sees List
's methods. However, when you do LinkedList<String> strings = new LinkedList<>();
java sees it as a LinkedList
and it will work when you call methods LinkedList
has like getLast
. People do List<String> strings = new LinkedList<>();
mainly for readability but it should be about the same.
答案2
得分: 0
对于局部变量来说,在这种情况下并不是特别重要。
List<String> strings = new LinkedList<>();
的主要好处是,如果需要的话,你可以简单地将 LinkedList 替换为 ArrayList 或任何其他 List 的实现。
这在类的“边缘”(参数、返回类型、字段)是最重要的,因为它使得你的 API 更加灵活,原因如下:
- 其他类不受你选择的 List 实现的限制,因为你的类将接受任何 List。
- 与你的类交互的其他类被强制不知道你的类内部选择使用哪种 List 实现,这意味着你可以随时从 ArrayList 切换到 LinkedList,反之亦然,而不会破坏类外部的代码。
英文:
For local variables, it doesn't matter all that much, at least not in this case.
The main benefit of List<String> strings = new LinkedList<>();
is that you could simply swap the LinkedList for an ArrayList or any other List implementation if needed.
This is most important at the "edges" of your class (parameters, return types, fields) because it makes your API more flexible for two reasons:
- Other classes are not bound by your choice of List implementation, because your class will take any List
- Other classes that interface with your class are forcibly unaware of your class' internal choice of which List implementation to use, which means you can switch from an ArrayList to a LinkedList or vice versa at any time, without breaking code outside of your class
答案3
得分: 0
来自《Effective Java》第3版,条款64:
通过其接口引用对象
如果你养成使用接口作为类型的习惯,你的程序将会更加灵活。如果你决定要切换实现,你只需要在构造函数中更改类名(或者使用不同的静态工厂)。
因此,使用 List<String> strings = new LinkedList<>();
,你只需要更改实现的名称,客户端代码的其余部分保持不变。
使用 LinkedList<String> strings = new LinkedList<>();
,你可能需要修改客户端代码。
需要注意的是,将实现类作为类型也是完全合适的。但是,如果你可以使用接口作为类型,那将是最佳选择。
英文:
From Effective Java, 3rd Edition, Item 64:
REFER TO OBJECTS BY THEIR INTERFACES
> If you get into the habit of using interfaces as types, your program
> will be much more flexible. If you decide that you want to switch
> implementations, all you have to do is change the class name in the
> constructor (or use a different static factory).
So, with List<String> strings = new LinkedList<>();
, you just have to change the implementation name and the rest of the client code remains same. <br>
<br>
With LinkedList<String> strings = new LinkedList<>();
, you may have to modify your client code.
Note that it's entirely appropriate to go ahead with implementation class as types. But, if you can use an interface as type, it's the best choice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论