接口的要点

发布于 2024-09-28 08:06:10 字数 326 浏览 8 评论 0原文

可能的重复:
我如何知道何时创建接口?

我想知道使用接口的意义。

你使用接口吗?如果是这样,您什么时候决定使用它们,什么时候决定不使用它们?

我目前已经为我的服务层和存储库层定义了接口,但我想知道我是否错过了它们有用的其他地方。

我想我只是不完全理解他们的目的。

Possible Duplicate:
How will I know when to create an interface?

I'm wondering about the point of using an Interface.

Do you use Interfaces? If so, when do you decide to use them and when do you decide NOT to use them?

I've currently got interfaces defined for my service layers and my repository layers, but I'm wondering if I'm missing out on other places where they'd be useful.

I guess I just don't fully understand their purpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花开半夏魅人心 2024-10-05 08:06:10

接口定义契约。任何实现接口的类都必须履行该契约。这意味着该类必须实现接口中定义的方法。

接口基本上表示“我正在定义所有实现者都必须执行的操作。我不关心您如何执行此操作,但您必须支持我指定的这些操作”。

接口的另一个用途是您可以在方法签名或类型定义中使用它们来指定对象的最通用类型。例如,在 Java 中,Map 是一个由其他类(例如 HashMapLinkedHashMap)实现的接口。 HashMapLinkedHashMap 本质上都是 Map 类型。它们实现相同的方法,但执行不同的操作(LinkedHashMap 保留插入顺序)。

考虑一下您有一个接受地图的方法的情况。如果没有接口,则需要为每种类型的映射指定一个方法。事实上,您可以通过重载方法来做到这一点,但这种方法不是很好。更好的方法是将方法参数的类型指定为Map。然后,任何实现 Map 的类都可以传递给该方法。这样,您不必为每种类型的映射指定方法,而且您也不会将使用您的方法的人限制为映射的特定实现。

接口还保证指定的功能存在于实现类中。因此,它还提供了访问该功能的标准方法。当您设计 API 时,接口也很有用(这样您就可以为要公开的内容指定标准接口)。

接口的另一个好处是它使重构变得容易。假设您想要切换某种对象的实现。该对象可能是方法参数,也可能是类属性。由于您已将该参数或对象键入为接口,因此您可以简单地创建一个实现该接口的新类并传递该类。由于您使用了该接口,因此您没有对类的细节做出额外的假设。该接口抽象出您正在使用的类的实现细节。这样,您最终就不会做出使您的代码与特定实现过于紧密耦合的假设。

总而言之,接口是关于抽象契约。通过抽象,您可以隐藏底层细节并仅公开您需要公开的最低限度的细节。这样,使用您的类或接口的人就不会受到实现细节的负担。所有这些信息都巧妙地隐藏在实现该接口的特定类中。 合同确保全面标准化;使用该接口的人确信实现该接口的所有类都公开相同的方法。

An interfaces defines a contract. Any class that implements an interface has to fulfil that contract. This means that the class must implement methods defined in the interface.

An interface basically says "I'm defining something that all implementers must do. I don't care how you do it, but you must support these operations that I've specified".

Another use of interfaces is that you can use them in method signatures or type definitions to specify the most generic type of an object. For example, in Java Map is an interface that is implemented by other classes like HashMap or LinkedHashMap. Both HashMap and LinkedHashMap are essentially of type Map. They implement the same methods, but they do things differently (LinkedHashMap preserves insertion-order).

Consider the situation where you have a method that accepts maps. If you didn't have interfaces, you would need to specify a method for each type of map. Indeed, you could do that via overloaded methods, but that approach is not very good. The better way is to specify the type of the method argument as Map. Then, any class that implements Map can be passed in to the method. This way, you don't have to specify a method for each type of map, and also you are not restricting the person who uses your method, to specific implementations of the map.

An interface also guarantees that the specified functionality is present in implementing classes. As such, it also provides a standard way to access that functionality. Interfaces are also useful when you are designing an API (that way you can specify a standard interface to the things you want to expose).

Another benefit of interfaces is that it makes refactoring easy. Let's say that you want to switch out an implementation of a some sort of object. The object might be a method argument or it may be a class property. Since you've typed that argument or object as an interface, you can simply create a new class that implements the interface and pass that class instead. Since you used the interface, you didn't make an extra assumptions as to the details of the class. The interface abstracts out the implementation details of the class that you're using. That way you don't end up making assumptions that makes your code too tightly-coupled to a specific implementation.

To sum it up, interfaces are about abstraction and contracts. With abstraction you hide the underlying details and expose only the bare minimum that you need to expose. That way the person who uses your class or interface is not burdened with the implementation details. All that information is neatly hidden inside the specific class that implements the interface. The contract ensures standardization across the board; a person who uses the interface is sure that all classes that implement the interface expose the same methods.

微暖i 2024-10-05 08:06:10

接口是用来告诉其他人一个类做某事的。

例如,如果您有一个实现 IInjurableSoccerPlayer 类 - 您从该类代码的第一行就知道,SoccerPlayer 实例知道要做什么受伤时要做的事情(你可能知道连字符之后)。

现在考虑一下实现 IEnumerableIQueryableIDisposable 告诉您有关对象的信息,而无需了解有关实现本身的任何信息..
好像很多..

An interface is for telling others a class does something.

e.g. If you have a SoccerPlayer class which Implements IInjurable - you know from the first line of the class' code, a SoccerPlayer instance knows what to do when injured (you probably knew that just after the hyphen).

Now consider what implementing IEnumerable , IQueryable or IDisposable tells you about an object, and that's without knowing anything about the implementation itself..
Seems like a lot..

抱着落日 2024-10-05 08:06:10

界面就像一个规则列表。您指定一个类的要求,并且对于任何实现此接口的类,您知道它们将遵循这些规则。您可以将这些类强制转换为此接口类型,并对它们进行操作,因为您知道它们具有您认为需要的方法、属性和事件。

ASP.Net 中的一些重要接口包括:

  • ICallbackEventHandler
  • IDisposable

最近,当我想确保用于特定目的的所有类都具有特定的方法和事件时,我创建了自己的接口。知道了这一点,我可以首先通过强制转换并验证来检查它是否实现了接口:

IMyInterface myinterface = myclass as IMyInterface;
if (myinterface == null)
{
    //did NOT implement the interface
}
else
{
    //did implement the interface
    //call the method we KNOW is there.
    myinterface.MyMethod(myparemeter);
}

An interface is like a list of rules. You designate the requirements of a class, and for any class that implements this interface, you know that they will follow these rules. You can cast those classes as this interface type, and operate on them knowing that they have the methods, properties, and events that you decided are required.

Some important interfaces in ASP.Net are:

  • ICallbackEventHandler
  • IDisposable

I created my own interface recently when I wanted to be sure all of the classes that were used for a specific purpose have a specific method and event. Knowing that, I could first check that it implements the interface by casting it and verifying:

IMyInterface myinterface = myclass as IMyInterface;
if (myinterface == null)
{
    //did NOT implement the interface
}
else
{
    //did implement the interface
    //call the method we KNOW is there.
    myinterface.MyMethod(myparemeter);
}
苏大泽ㄣ 2024-10-05 08:06:10

许多其他答案忽略的一点是,接口允许非常有限的多重继承形式,因为两个或多个不相关的类可以实现一个公共接口,并且代码可以接受实现该接口的对象,而不考虑其类型的其他任何内容。否则,代码接受多种类型的对象并利用任何常见功能的唯一方法是它们都源自本身实现此类功能的公共基本类型。

One point many other answers miss is that interfaces allow for a very limited form of multiple inheritance, in that two or more unrelated classes can implement a common interface, and code can accept objects which implement that interface without regard for anything else about their type. Otherwise, the only way for code to accept multiple types of objects and exploit any common functionality would be for them to all descend from a common base type which itself implemented such functionality.

人疚 2024-10-05 08:06:10

正如其他人所说,接口定义了可以由类和结构实现的契约。这使得像对象继承这样的接口能够实现多态性。

然而,实现接口与从对象继承不同,因为

  1. 结构可以实现它们。
  2. 接口不能有实现。
  3. 可以实现多个接口。这实现了一种多重继承,而没有相关的问题(无论好坏,C# 都没有实现多重继承)

所以一般来说,如果你想要多态性并且你想要它用于

  1. 结构
  2. 共享实现,那么接口是好的,但共享实现没有意义或可能导致对于脆弱的基类,
  3. 目标类上已经存在对象继承。

标准示例是 IDisposable、IComparable 和 IEnumerable,并显示了最明显的用途。

要避免的一些事情是标记接口(没有方法的接口)和不受支持的接口。由系统。例如,您有 IPost、BasePost、问题、答案和评论,但系统仅使用 BasePost。

As others have said an interface defines a contract that can be implemented by classes and structs. This allows Interfaces like object inheritance to enable polymorphism.

However implmenting an interface differs from inheriting from an object in that

  1. Structs can implmentent them.
  2. Interfaces cannot have implmentaions.
  3. More than one interface can be implemented. Which enables a kind of multiple enheritance without the assoicated problems (for good or ill C# doesn't implement multiple inheritance)

So in general interfaces are good if you want polymorphism and you want it for

  1. structs
  2. shared implmentation doesn't make sense or could lead to a brittle base clas
  3. there already exists a object inheritance on your target class

Standard examples are IDisposable, IComparable and IEnumerable and show the most obvious uses.

Some things to avoid are marker interfaces (interfaces without methods) and interfaces that aren't supported. By the system. e.g. You have IPost, BasePost, Question, Answer and Comment, but the system only ever uses BasePost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文