Java 中本地集合的类型约定
我最近遇到了一组实例化本地映射的代码,如下所示:
HashMap<String, Object> theMap = new HashMap<String, Object>();
通常,当我看到使用 HashMap(并且我自己也使用它们)时,本地变量只是 Map
(接口),而不是而不是与具体实施挂钩。显然,如果Map
可能被实例化为各种Map
类型(例如接受参数),那么这是必需的。然而,在类似上面的情况下,它是在同一点定义和实例化的,是否存在仅使用接口类型的根本原因,或者只是样式/约定?
I recently ran across a set of code which instantiated local maps as following:
HashMap<String, Object> theMap = new HashMap<String, Object>();
Typically, when I've seen HashMaps used (and used them myself), the local variables are simply Map
(the interface), rather than being tied to the specific implementation. Obviously this is required if the Map
could potentially be instantiated as various Map
types (e.g. accepting a parameter). However, in the case of something like the above where it's defined and instantiated at the same point, is there an underlying reason to only use the interface type, or is it simply style/convention?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(我最初根据标题误解了这个问题,但我已经包含了类型和变量约定,因为两者都很有趣。)
重要的是它是一个地图:您可以在其中查找内容。剩下的就是实现细节。
我建议给它一个语义名称,例如
......这样当您阅读代码时,您就会知道键和值的含义。
至于变量的类型 - 同样,我通常会使用接口而不是实现,部分原因是它表明我没有使用特定于该类型的任何成员。我不想强调代码中的实现,通常......这意味着当我确实关心实现时,我可以使其更加明显。
(I originally misunderstood the question based on the title, but I've included both type and variable conventions as both are interesting.)
What's important is that it's a map: something you look things up in. The rest is an implementation detail.
I would suggest giving it a semantic name, e.g.
... that way when you read the code, you'll know what the keys and values are meant to be.
As for the type of the variable - again, I'd typically use the interface rather than the implementation partly because it indicates I'm not using any members which are specific to the type. I don't want to emphasize the implementation in the code, usually... it means when I do care about the implementation, I can make that more obvious.
将对象声明为
Map
将允许编译器保护您免于调用特定于HashMap
的方法。这将允许您将来替换另一个Map
实现,而不必担心Map
接口中不存在的方法调用。Declaring the object as a
Map
will allow the compiler to protect you from calling methods which are specific toHashMap
. This will allow you to substitute anotherMap
implementation in the future without worrying about having method calls which do not exist in theMap
interface.一般来说,人们主要使用 Map 来对实现做出最少的假设。
类名不可能用于其他方法,因为 HashMap 仅添加了clone(),而 HashMap 已不再使用(有充分的理由)。
可能的情况是,由于某种原因,映射需要可序列化,并且普通的 Map 接口不会扩展它,但 HashMap 确实实现了它。
In general people use mostly Map to make the least amount of assumptions on the implementation.
It cannot be that the Classname is used for the additional methods as only clone() is added by HashMap, which has fallen in disuse (for good reasons).
What could be is that the map needs to be Serializable for one reason or another, and the plain Map interface does not extend it, but HashMap does implement it.
即使在这种情况下,它也保持通用。接口编码可确保您使用的是
Map
而不是它的特定实现。Even in this case, it keeps it generic. Coding to interface ensures you are using a
Map
and not a specific implementation of it.