您的位置:首页 > 其它

使用集合接口的时候应该使用通用类型代替具体的实现类型

2015-12-28 09:04 447 查看
Sonar静态代码检查发现下面缺陷:

Declarations should use Java collection interfaces such as “List” rather than specific implementation classes such as “LinkedList”

The purpose of the Java Collections API is to provide a well defined hierarchy of interfaces in order to hide all implementation details.

Implementating classes must be used to instantiate a new collection, but the result of an instantiation should immediately be stored in a variable whose type is a

Java Collection interface

就是说譬如你要实现一个HashMap的集合,不用直接用 HashMap hm = new HashMap();这样的方式去实例化,而是要用 Map hm = new HashMap();来做。

同理LinkedList的实例化也是 List ll = new LinkedList();这样的方式去实例化
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: