为什么编码应该是“到接口”的?特别是对于原始数据类型? [英] Why the coding should be "to the interface" especially for primitive datatypes?

查看:114
本文介绍了为什么编码应该是“到接口”的?特别是对于原始数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对设计方法有疑问,为什么我们将代码实现到接口。这在原始数据类型中非常常见。就像我没有得到这两者之间的区别:

I have a doubt on the design methodology that why we implement the code to the interface. This is very much observed in primitive data types. Like I am not getting the difference between these two :

Map<Integer, String> mymap = new HashMap<Integer, String>(); 

HashMap<Integer, String> mymap = new HashMap<Integer, String>();

这两者有什么区别吗?我的意思是我们将要使用的每个地方 mymap 在两种情况下都会保持相同。

Is there any difference between these two? I mean each and every place where we are going to use mymap will remain same in both the cases.

I很抱歉,如果这个问题似乎毫无用处,但我真的没有意识到这将如何在以后使用 mymap 的地方产生任何影响。请帮帮忙?

I am sorry if the question seems to be of no use but I really not getting that how this is going to make any difference later on where mymap will be used. Please help?

谢谢..

注意 - 我已经看过这个关于SO的问题,但它没有给出我想要的东西。

Note - I have already seen this question on SO but it is not giving what I want.

推荐答案

因为mymap可以在其他地方使用 Map 的不同实现进行实例化,所以你不应该依赖它作为 HashMap的实例

Because mymap can be instantiated somewhere else with a different implementation of Map, you should not rely on it being an instance of HashMap in the code using it.

这篇关于为什么编码应该是“到接口”的?特别是对于原始数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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