• C#类、接口、虚方法和抽象方法接口与抽象类的区别实例


    1.抽象类可以有实现(包括构造函数),而接口不可以有任何实现。

    namespace ConsoleApplication1
    {
        class Program
        {
            interface IMyInterface1
            {
                void IMethod1();
                void IMethod2();
            }
            abstract class AMyClass1
            {
                public abstract void AMethod1();
                public abstract void AMethod2();
                public AMyClass1()
                {
                }
                public void AMethod()
                {
                    Console .WriteLine("AMyClass.Amethod." );
                }
            }
            static void Main(string [] args)
            {
            }
        }
    }

    在上面我们定义了一个接口和一个抽象类,在抽象类中我们有一个实现方法 AMyClass1 而在接口中这是不允许的。接口中的所有方法都必须是未实现的(包括构造函数也是不能有的)。

    2. 抽象类中还以有成员变量(包含静态成员变量)、属性、常量和静态方法,并且他们可以是非公共的;而接口中不能有成员变量、常量、静态方法,只能有公共的属性。

    namespace ConsoleApplication1
    {
        class Program
        {
            interface IMyInterface1
            {
                void IMethod1();
                void IMethod2();
                int IProperty1
                {
                    set ;
                    get ;
                }
            }
            abstract class AMyClass1
            {
                public abstract void AMethod1();
                public abstract void AMethod2();
                public void AMethod3()
                {
                    Console .WriteLine("AMyClass.Amethod." );
                }
                private static void AMethod4()
                {
                }
                private int i;
                private static int j;
                private double PI = 3.1514926;
                private int aProperty;
                int Aproperty
                {
                    get { return aProperty; }
                    set { aProperty = value ; }
                }
            }
            static void Main(string [] args)
            {
            }
        }
    }

    正如上面的一段代码一样,在抽象类中可以有属性、常量、成员变量(包含静态成员)、静态方法,而且它们还可以是非公共的;而在接口中除了有未实现的公共方法外,只可以有属性,并且是公共的(默认,不能添加public 修饰符,否则在C# 中报错)。

    3.抽象类可以从另一个类或者一个/ 多个接口派生;而接口不能从另一个类派生却可以实现另一个或多个接口。

    namespace ConsoleApplication1
    {
        class Program
        {
            interface IMyInterface1
            {
                void IMethod1();
                void IMethod2();
                int IProperty1
                {
                    set ;
                    get ;
                }
            }
            interface IMyInterface2
            {
                void IMethod3();
                void IMethod4();
            }
            abstract class AMyClass1
            {
                public abstract void AMethod1();
                public abstract void AMethod2();
                public void AMethod3()
                {
                    Console .WriteLine("AMyClass.Amethod." );
                }
                private static void AMethod4()
                {
                }
                private int i;
                private static int j;
                private double PI = 3.1514926;
                private int aProperty;
                int Aproperty
                {
                    get { return aProperty; }
                    set { aProperty = value ; }
                }
            }
            abstract class AMyClass2
            {
                public abstract void AMethod3();
                public abstract void AMethod4();
            }
            abstract class AMyClass3 : AMyClass1
            {
                public override void AMethod1()
                {
                }
                public override void AMethod2()
                {
                }
            }
            abstract class AMyClass4 : AMyClass1 ,IMyInterface1 ,IMyInterface2
            {
                public override void AMethod1()
                {
                }
                public override void AMethod2()
                {
                }
                public void IMethod1()
                {
                }
                public void IMethod2()
                {
                }
                public int IProperty1
                {
                    get
                    {
                        throw new NotImplementedException ();
                    }
                    set
                    {
                        throw new NotImplementedException ();
                    }
                }
                public void IMethod3()
                {
                }
                public void IMethod4()
                {
                }
            }
            interface IMyInterface3 : IMyInterface1 , IMyInterface2
            {
            }
            static void Main(string [] args)
            {
            }
        }
    }

    抽象类可以继承类一个类,而不能继承多个类。但是可以实现多个接口,而且可以在继承一个类的同时实现一个或多个接口;接口则只能实现其他的一个或多个接口,不能继承其他类。

    4.一个.net 类只能从一个基类中派生即使这个基类是抽象类;然而一个.net 类可以实现多个接口。(但是如果从一个抽象类派生或者从一个/ 多个接口实现,则必须实现抽象类或接口中所有的方法)

    namespace ConsoleApplication1
    {
        class Program
        {
            interface IMyInterface1
            {
                void IMethod1();
                void IMethod2();
                int IProperty1
                {
                    set ;
                    get ;
                }
            }
            interface IMyInterface2
            {
                void IMethod3();
                void IMethod4();
            }
            abstract class AMyClass1
            {
                public abstract void AMethod1();
                public abstract void AMethod2();
                public void AMethod3()
                {
                    Console .WriteLine("AMyClass.Amethod." );
                }
                private static void AMethod4()
                {
                }
                private int i;
                private static int j;
                private double PI = 3.1514926;
                private int aProperty;
                int Aproperty
                {
                    get { return aProperty; }
                    set { aProperty = value ; }
                }
            }
            abstract class AMyClass2
            {
                public abstract void AMethod3();
                public abstract void AMethod4();
            }
            class Test1 : AMyClass1
            {
                public override void AMethod1()
                {
                }
                public override void AMethod2()
                {
                }
            }
            class Test2 : IMyInterface1 , IMyInterface2
            {
                public void IMethod1()
                {
                }
                public void IMethod2()
                {
                }
                public int IProperty1
                {
                    get
                    {
                        throw new NotImplementedException ();
                    }
                    set
                    {
                        throw new NotImplementedException ();
                    }
                }
                public void IMethod3()
                {
                }
                public void IMethod4()
                {
                }
            }
            static void Main(string [] args)
            {
            }
        }
    }

    从上面的我们可以看出,C# 是不支持多继承的,如果需要多继承我们可以用接口实现(注意在一个类即实现接口又继承其他父类时,接口要在后面)。

  • 相关阅读:
    使用youtube-dl下载B站视频
    【北邮人论坛帖子备份】14 考公与考研、入党、秋招
    观《菊次郎的夏天》有感
    git远端分支改名并以当前分支为base新建分支
    依赖明明存在pom.xml却报Dependency 'groupId:artifactId:version' not found的错
    在服务器上搭建git服务
    【北邮人论坛帖子备份】【秋招】21届渣硕BAT后端研发上岸心得
    【北邮人论坛帖子备份】【心得】做科研写论文的一些小经验
    【北邮人论坛帖子备份】 图森同学 | Gala:一个北邮非典型学霸的成长之路
    Python-for循环
  • 原文地址:https://www.cnblogs.com/weichao975/p/1775087.html
Copyright © 2020-2023  润新知