问题描述
我总是知道当我们声明一个我们应该做的集合时,Interface ob = new Class()
,如果我想使用例如一个LinkedList,我会做List ob = newLinkedList()
,但随后我无法访问 LinkedList 中的所有方法.LinkedList ob = new LinkedList()
不是 100% 正确吗?
I always learn when we declare a collection we should do, Interface ob = new Class()
, if i want to use for example a LinkedList i'll do List ob = new LinkedList()
, but then i can't have access to all methods from LinkedList.. Isn't LinkedList ob = new LinkedList()
100% correct?
推荐答案
好吧,我建议使用通用形式,但可以肯定 - 如果您想使用 特定 到 LinkedList
的功能,您需要相应地声明变量.
Well I'd suggest using the generic form, but sure - if you want to use functionality which is specific to LinkedList
, you need to declare the variable accordingly.
您可能想要检查 Deque
或 Queue
接口有你想要的.如果他们这样做,请使用那些与描述您需要什么而不是您将使用什么实现的想法保持一致的内容.
You might want to check whether the Deque<E>
or Queue<E>
interfaces have what you want though. If they do, use those in-keeping with the idea of describing what you need rather than what implementation you'll use.
这篇关于在 Java 中声明一个 LinkedList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!