gpt4 book ai didi

c# - 为什么一个接口(interface)会实现另一个接口(interface)?

转载 作者:可可西里 更新时间:2023-11-01 08:03:46 25 4
gpt4 key购买 nike

我正在查看 List<T> 的声明并看到它实现了 IList<T> , ICollection<T>IEnumerable<T> (除其他外)。

然后我去看IList<T>的定义并看到它实现了 ICollection<T>IEnumerable<T> .

如果一个接口(interface)实现另一个接口(interface)就像“契约(Contract)”一样工作,而我们没有为它们编写真正的代码,那有什么意义呢?

这个实现是累积的吗?如果是,因为 IList<T>工具 ICollection<T>IEnumerable<T> , List<T>不应该只实现 IList<T>

抱歉,如果我的问题令人困惑,我现在有点困惑。

最佳答案

What's the point of an interface implementing another interface if they work just as "contracts" and we write no real code at them?

接口(interface)支持继承并以与类相同的方式促进多态性。一个IList<T>也是一个 ICollection<T> , 与 TextBox 相同是 Control .

Is this implementation cumulative? If it is, since IList implements ICollection and IEnumerable, List shouldn't implement only IList?

正如其他人所指出的,这主要是为了提高可读性和可维护性。是的,继承是累积的。但是,当一个类实现多个包含具有相同签名的成员的接口(interface)时,它只需要为所有实现定义一个成员。 Explicit interface implementation可用于为给定的实现显式定义成员。

关于c# - 为什么一个接口(interface)会实现另一个接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24945345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com