为什么代码经常写成这样:
Set<E> set = new HashSet<E>();
List<E> list = new ArrayList<E>();
而不是: Set<E> set = new Set<E>();
List<E> list = new List<E>();
Set<E> set = new HashSet<E>();
List<E> list = new ArrayList<E>();
而不是: Set<E> set = new Set<E>();
List<E> list = new List<E>();
之所以写成:
Set<E> set = new HashSet<E>();
List<E> list = new ArrayList<E>();
因为这样有利于扩展,面向接口编程(Spring就是这样)。
打个比方,你什么时候想更改后面的类了,前面的接口不用换,不然前面的接口也得跟着修改。
既然地球人都这么做,你也就顺流吧...
好处是 易于代码的维护楼主可能是想问这个吧
ArrayList <E> list = new ArrayList <E>();
引用楼上的:打个比方,你什么时候想更改后面的类了,前面的接口不用换,不然前面的接口也得跟着修改。