为什么我们应该使用接口,而不是具体的类型? [英] Why we should use Interface, instead of concrete types?

查看:180
本文介绍了为什么我们应该使用接口,而不是具体的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Java集合,我们建议使用,而不是具体的接口类型。

When using collections in Java, we are advised to use Interface instead of concrete types.

我爱:列表<对象>名单=新的ArrayList<对象>();

不过,使用的ArrayList<对象>名单=新的ArrayList<对象>(); 也会做同样的工作,对

But, using ArrayList<Object> list = new ArrayList<Object>(); will also does the same job, right?

推荐答案

是的,但如果你以后改变主意,并使用的LinkedList 您必须更改多您code。

Yes, but if you later change your mind and use a LinkedList You have to change much more in your code.

这篇关于为什么我们应该使用接口,而不是具体的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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