4

我想知道我们什么时候可以使用扩展另一个接口的接口。我想知道一个实际的例子以及我们何时使用它。

4

3 回答 3

7

当子接口提供了超接口所提供的一切,并且做一些其他重要的事情时,你就扩展了一个接口。比如SortedMap<K,V>implements Map<K,V>,因为 sorted map 是一个 map,它支持 map 的所有操作,加上一些只适用于 sorted map 的操作。

这类似于类之间的继承,但它允许多种实现。例如,可以将 a 实现SortedMap为键的排序列表加上值的并行数组,而不是树。这将让用户在不更改其余代码的情况下交换更快或更好的实现。换句话说,接口之间的继承可以让您保留对接口进行编程的好处。

于 2012-05-17T18:20:02.860 回答
2

查看类似的接口java.util.Collectionjava.util.Set了解这是如何完成的,以及如何收紧合约。

于 2012-05-17T18:18:30.417 回答
0
  1. When we want to use multiple inheritance in our application then one interface should extends other interface.

  2. To make the parallel development of your application it is very necessary to write your code in such a way that you can incorporate newly discovered requirements into the existing code as painlessly as possible. So, if we implements the interface then concrete class names locks you into specific implementations, making down-the-line changes unnecessarily difficult. Therefore, we extends interface.

于 2012-05-17T18:54:12.673 回答