C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例.docx_第1页
C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例.docx_第2页
C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例.docx_第3页
C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例.docx_第4页
C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论