问题描述
我有以下代码:
ArrayList<延伸IValues> valuesList
IValues 是接口。
该列表中充满了实现 IValues 接口的类/对象。
我理解这个列表中每个对象的概念必须满足他们实现的接口规定的合约。
为什么java语言不这样做:
ArrayList
?这对用户来说会更加清楚,不是吗?
使用哪个关键字的语法选择最有可能在这种情况下任意。这只是一个标记,表示类型参数必须是 IValues 的子类型。这是用 extends 来继承, implements 用于接口。
个人而言,我发现最好使用一个关键字来代替两个不同的类和接口。并且 extends 是一个不错的选择,尤其是当您在界面中使用它时:
interface ISpecialValues extends IValues {
...
}
I have the following code:
ArrayList<? extends IValues> valuesList
IValues is an interface.The list is filled with classes/objects that implement the IValues interface.I do understand the the concept of every object in this list having to fulfill the contract laid down by the interface they implement.Why doesn't the java language do this like this:
ArrayList<? implements IValues> valuesList
? This would be much clearer to the user, wouldn't it?
The syntactical choice of which keyword to use is most likely arbitrary in this case. It's just a marker saying that the type argument must be a subtype of IValues. This is written with extends for inheritance and implements for interfaces.
Personally I find it better to use one keyword for both classes and interfaces instead of two different ones. And extends is a good choice, especially since you use it in interfaces as well:
interface ISpecialValues extends IValues { ... }
这篇关于为什么<?扩展界面>而不是<?实现界面>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!