为什么接口不能包含实现?

时间:2022-01-28 21:03:32

Why does an interface not contain any implementation for its members? Was there any other reason for it only declaring members apart from being able to use it as a sort of contract?

为什么接口不包含其成员的任何实现?除了能够将其用作某种合同之外,还有其他原因吗?

The question is not for what is Interface or why we need Interface. The Question is Why we have a design that contains only Declaration. I don't think there is lot of similar ques. I cant find a relevant ans, so raised a new post.

问题不在于什么是接口或为什么我们需要接口。问题是为什么我们的设计只包含声明。我不认为有很多类似的问题。我找不到相关的ans,所以提出了一个新帖子。

3 个解决方案

#1


3  

First, the assertion that interface members cannot contain implementations is not entirely correct: although you cannot write an implementation in the interface itself, you can share implementations of interface "outside members" by placing them in an extension method for an interface. LINQ library derives a good deal of its power from an ability to provide method implementations for interfaces, not necessarily for classes.

首先,接口成员不能包含实现的断言并不完全正确:尽管您不能在接口本身中编写实现,但您可以通过将接口放在接口的扩展方法中来共享接口“外部成员”的实现。 LINQ库从为接口提供方法实现的能力中获得了大量的功能,而不一定是类。

In general, though, interfaces provide a mechanism of separating the contract from its implementations by defining what is done without specifying how it is done. This separation is very powerful, because it lets you share code that programs to an interface. For example, you can write code to filter a collection only once, and then reuse the same code for collections of multiple types.

但是,一般而言,接口提供了一种机制,通过定义完成的内容而不指定合同的完成方式,将合同与其实现分离。这种分离非常强大,因为它允许您共享编程到接口的代码。例如,您可以编写代码仅过滤一次集合,然后对多个类型的集合重用相同的代码。

#2


2  

It was decided very early on in the design of .NET to not allow for multiple inheritance.

在.NET的设计中很早就决定不允许多重继承。

Had they allowed it then this would have been legal:

如果他们允许,那么这将是合法的:

class A
{
    void Blah() { /* body */ }
}

class B
{
    void Blah() { /* body */ }
}

class C : A, B
{
}

The problem is which Blah should class C inherit.

问题是Blah应该继承C类。

This problem is basically the same if interfaces had implementations. It would become this:

如果接口有实现,这个问题基本相同。它会变成这样:

class IA
{
    void Blah() { /* body */ }
}

class IB
{
    void Blah() { /* body */ }
}

class C : IA, IB
{
}

Instead by not allowing interfaces to have bodies then interfaces become a safe mechanism to allow objects to have polymorphism similar to multiple inheritance without any of the issues associated with full multiple inheritance.

相反,通过不允许接口具有主体,接口成为一种安全机制,允许对象具有类似于多重继承的多态性,而没有任何与完全多重继承相关的问题。

#3


2  

There are two purposes for interfaces:

接口有两个用途:

  • as you already said: for establishing a contract of the use of an object;
  • 正如你已经说过的那样:建立一个使用对象的合同;
  • interfaces can also be used as marker. It does not need members for that.
  • 接口也可以用作标记。它不需要成员。

Consider this sample:

考虑这个样本:

public class X : IMustSerialize
{ }

This class implements the memberless interface IMustSerialize. When walking down all classes or instances you can check whether the class implements this interface and act accordingly.

此类实现无成员接口IMustSerialize。在遍历所有类或实例时,您可以检查该类是否实现此接口并相应地执行操作。

Also see What is the purpose of a marker interface?.

另请参阅标记界面的用途是什么?

#1


3  

First, the assertion that interface members cannot contain implementations is not entirely correct: although you cannot write an implementation in the interface itself, you can share implementations of interface "outside members" by placing them in an extension method for an interface. LINQ library derives a good deal of its power from an ability to provide method implementations for interfaces, not necessarily for classes.

首先,接口成员不能包含实现的断言并不完全正确:尽管您不能在接口本身中编写实现,但您可以通过将接口放在接口的扩展方法中来共享接口“外部成员”的实现。 LINQ库从为接口提供方法实现的能力中获得了大量的功能,而不一定是类。

In general, though, interfaces provide a mechanism of separating the contract from its implementations by defining what is done without specifying how it is done. This separation is very powerful, because it lets you share code that programs to an interface. For example, you can write code to filter a collection only once, and then reuse the same code for collections of multiple types.

但是,一般而言,接口提供了一种机制,通过定义完成的内容而不指定合同的完成方式,将合同与其实现分离。这种分离非常强大,因为它允许您共享编程到接口的代码。例如,您可以编写代码仅过滤一次集合,然后对多个类型的集合重用相同的代码。

#2


2  

It was decided very early on in the design of .NET to not allow for multiple inheritance.

在.NET的设计中很早就决定不允许多重继承。

Had they allowed it then this would have been legal:

如果他们允许,那么这将是合法的:

class A
{
    void Blah() { /* body */ }
}

class B
{
    void Blah() { /* body */ }
}

class C : A, B
{
}

The problem is which Blah should class C inherit.

问题是Blah应该继承C类。

This problem is basically the same if interfaces had implementations. It would become this:

如果接口有实现,这个问题基本相同。它会变成这样:

class IA
{
    void Blah() { /* body */ }
}

class IB
{
    void Blah() { /* body */ }
}

class C : IA, IB
{
}

Instead by not allowing interfaces to have bodies then interfaces become a safe mechanism to allow objects to have polymorphism similar to multiple inheritance without any of the issues associated with full multiple inheritance.

相反,通过不允许接口具有主体,接口成为一种安全机制,允许对象具有类似于多重继承的多态性,而没有任何与完全多重继承相关的问题。

#3


2  

There are two purposes for interfaces:

接口有两个用途:

  • as you already said: for establishing a contract of the use of an object;
  • 正如你已经说过的那样:建立一个使用对象的合同;
  • interfaces can also be used as marker. It does not need members for that.
  • 接口也可以用作标记。它不需要成员。

Consider this sample:

考虑这个样本:

public class X : IMustSerialize
{ }

This class implements the memberless interface IMustSerialize. When walking down all classes or instances you can check whether the class implements this interface and act accordingly.

此类实现无成员接口IMustSerialize。在遍历所有类或实例时,您可以检查该类是否实现此接口并相应地执行操作。

Also see What is the purpose of a marker interface?.

另请参阅标记界面的用途是什么?