本文介绍了C#黑客:接口和抽象类之间的低层次区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于C#基本面一个哲学问题:我想知道如何关闭的接口可以充分抽象类进行模拟。假设我们有以下接口:

 公共接口INativeInterface 
{
的performAction无效();
String属性{搞定;组; }
}

和下面的抽象类:

 公共抽象类ISimulatedInterface 
{
公共抽象无效的performAction();
公共抽象String属性{搞定;组; }
}



他们有如此多的共同点,是不是?我知道它们的区别是:




  • 多重继承不会为抽象类

  • 工作
  • 明确实施不使用反射或类似的东西呢?


    $工作抽象类



可以将这些限制被忽略b $ b

我认识到,接口和抽象类的根源是不同的:接口声明的一个条件可以表现得像,抽象类 - 是一种,但即使这似乎是如此接近,这些实体之间的低电平差必须讨论。这个问题甚至可以听起来像你会做什么,以使C ++的接口。


解决方案

No. Abstract classes and interfaces are different concepts at the Common Language Runtime level. Hence it is neither possible in C# because of being ultimately limited be CLR's boundaries.

It is doable, but requires support (or 'allowance') from the underlying execution evironment (be it physical or managed).

In the past I designed a language that completely substituted abstract classes for interfaces. And yes, it did support multiple inheritance of such 'interfaces'. However, the implementation peculiarities are probably not worth the effort. The major 'low-level difference' was that an internal, embedded instance of the inherited abstract class had to be kept within the implementing class'es instance, and a chain of this pointers had to be maintained. Needless to say, it was a joyful experience :-)

这篇关于C#黑客:接口和抽象类之间的低层次区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:30