gpt4 book ai didi

c# - 界面设计?我可以迭代地做吗?我应该如何处理界面的变化?

转载 作者:行者123 更新时间:2023-11-30 07:39:14 25 4
gpt4 key购买 nike

在 C# 或 Java 中定义接口(interface)的最佳方法是什么?我们是否需要在真正需要时制作通用方法或添加方法?

问候,斯里尼瓦斯

最佳答案

一旦接口(interface)被定义,它就不会被改变。您必须考虑界面的用途并尽可能完整。

如果您以后发现需要添加一个方法,那么您真的应该定义一个新接口(interface),可能是一个 _V2 接口(interface),带有附加方法。

附录:Here you will find some good guidelines on interface design in C# ,作为一般 C# 设计的更大的、有值(value)的工作的一部分。它通常也适用于 Java。

摘录:

Although most APIs are best modeled using classes and structs, there are cases in which interfaces are more appropriate or are the only option.
DO provide at least one type that is an implementation of an interface. This helps to validate the design of the interface. For example, System.Collections.ArrayList is an implementation of the System.Collections.IList interface.
DO provide at least one API consuming each interface you define (a method taking the interface as a parameter or a property typed as the interface). This helps to validate the interface design. For example, List.Sort consumes IComparer interface.
DO NOT add members to an interface that has previously shipped. Doing so would break implementations of the interface. You should create a new interface to avoid versioning problems.

我推荐靠the broad type design guidelines .

关于c# - 界面设计?我可以迭代地做吗?我应该如何处理界面的变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/937380/

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