Bright Leopold

i come from the other world,i will go back after the love,the regret,the alive and the dead are over

导航

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# 是不支持多继承的,如果需要多继承我们可以用接口实现(注意在一个类即实现接口又继承其他父类时,接口要在后面)。

posted on 2010-07-11 07:57  Bright Leopold  阅读(1086)  评论(1编辑  收藏  举报