简体   繁体   English

继承和接口实现机制之间的本质区别是什么?

[英]What's is the essential difference between inheritance and interface implementation mechanisms?

Since interface is actually a type, I always regard interface implementation as a special kind of inheritance mechanism, treating the interface as the base type and the type implementing it as a derived type. 由于接口实际上是一种类型,因此我始终将接口实现视为一种特殊的继承机制,将接口视为基本类型,并将接口实现为派生类型。 When an instance of the 'derived' type is created, the methods defined in the 'base' type, I mean the interface, are added into the method table, then the methods defined in this 'derived' type itself are added into the method table too. 创建“派生”类型的实例时,将“基本”类型(即接口)中定义的方法添加到方法表中,然后将此“派生”类型本身定义的方法添加到方法中表也​​。 When there is a 'grandson' type, it will add all methods defined in its father and grandfather(the interface) declaration into its method table. 当存在“孙子”类型时,它将在其父项和祖父(接口)声明中定义的所有方法添加到其方法表中。 Virtual methods can be overridden of course. 虚方法当然可以被覆盖。

Is my interpretion correct? 我的解释正确吗? It seems not to make sense in this scenerio: What if the grandson implements the interface again? 在这个场景中似乎没有任何意义:如果孙子再次实现该接口怎么办? like: 喜欢:

interface IFather { void m(); }

class Son: IFather{}

class Grandson : Son, IFather{}

The Grandson adds the methods in the interface into its method table twice? Grandson将接口中的方法两次添加到其方法表中?

What if the grandson implements the interface again? 如果孙子再次实现该接口怎么办? like: 喜欢:

Run the below code, with and without Grandson implementing IFather and compare the results. 在有和没有Grandson实现IFather情况下,运行以下代码,并比较结果。

If Grandson implements IFather : 如果Grandson实施IFather

Son Son Son Son Grandson Grandson Son Grandson Grandson Grandson 孙子孙子孙子孙子孙子孙子

If Grandson does not implement IFather : 如果Grandson未实施IFather

Son Son Son Son Grandson Grandson Son Grandson Son Grandson 孙子孙子孙子孙子孙子孙子

Thus, the main scenario in which explicity mentioning that Grandson implements IFather makes a difference in when Grandson shadows / hides a property / method from Son ( TypeName in this case). 因此,在其中明确地提的是,主场景Grandson实现IFather使得当差Grandson 阴影/隐藏从属性/方法SonTypeName在这种情况下)。 Use of shadowing / hiding is quite uncommon, so in reality there is rarely any difference - specifying that you implement the interface twice generally has no impact (it acts the same as if you specified it once). 使用阴影/隐藏是非常少见的,因此实际上几乎没有什么区别-指定两次实现该接口通常没有任何影响(它的作用与一次指定的作用相同)。

using System;

namespace Bob
{
    public interface IFather
    {
        string TypeName { get; }
        string OverridableTypeName { get; }
    }

    public class Son : IFather
    {
        public string TypeName { get; } = "Son";
        public virtual string OverridableTypeName { get; } = "Son";
    }

    public class Grandson : Son //, IFather
    {
        public string TypeName { get; } = "Grandson";
        public override string OverridableTypeName { get; } = "Grandson";
    }

    public class Program
    {
        static void Main(string[] args)
        {
            var son = new Son();

            Console.WriteLine(son.TypeName);
            Console.WriteLine(son.OverridableTypeName);
            Console.WriteLine(((IFather)son).TypeName);
            Console.WriteLine(((IFather)son).OverridableTypeName);

            var grandson = new Grandson();

            Console.WriteLine(grandson.TypeName);
            Console.WriteLine(grandson.OverridableTypeName);
            Console.WriteLine(((Son)grandson).TypeName);
            Console.WriteLine(((Son)grandson).OverridableTypeName);
            Console.WriteLine(((IFather)grandson).TypeName);
            Console.WriteLine(((IFather)grandson).OverridableTypeName);

            Console.ReadLine();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM