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

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() 

posted @ 2013-07-15 14:47  mimo0  阅读(191)  评论(0)    收藏  举报