抽象类和接口有什么区别?
2013-04-16 13:04
309 查看
抽象类的成员可以具有访问级别,而接口的成员全部 public 级别;
抽象类可以包含字段,而接口不可以;
抽象类可以继承接口,而接口不能继承抽象类;
抽象类的成员可以具有具体实现,而接口不行;
抽象的子类可以选择性实现其基类的抽象方法,而接口的子类必须实现。
View Code
抽象类可以包含字段,而接口不可以;
抽象类可以继承接口,而接口不能继承抽象类;
抽象类的成员可以具有具体实现,而接口不行;
抽象的子类可以选择性实现其基类的抽象方法,而接口的子类必须实现。
View Code
// abstract_keyword.cs // Abstract Classes using System; interface IBaseClass { // 接口不能有实现 // Error: 'IBaseClass.AbstractMethod1()': interface members cannot have a definition // void AbstractMethod1() { } // 接口的成员全部public级别 // Error: The modifier 'public' is not valid for this item // public void IAbstractMethod(); void AbstractMethod(); // 接口不可以包含字段 // Error: Interfaces cannot contain fields // protected int _x = 100; } // Error: 'BaseClass2' does not implement interface member 'IBaseClass.AbstractMethod()' //abstract class BaseClass2 : IBaseClass //{ //} // 抽象类可以继承接口 // 接口的子类必须实现接口里的方法 abstract class BaseClass1 : IBaseClass { public abstract void AbstractMethod(); } // 接口不能继承抽象类 // Error: Type 'BaseClass' in interface list is not an interface //interface IBaseClass1 : BaseClass //{ //} abstract class BaseClass // Abstract class { // 抽象类的成员可以具有访问级别 int i = 1; protected int _x = 100; // 抽象类可以包含字段 protected int _y = 150; // 抽象类的成员可以具有具体实现 public void AbstractMethod(string s) { Console.WriteLine(s); } public abstract void AbstractMethod(); // Abstract method public abstract int X { get; } public abstract int Y { get; } } // 抽象的子类可以选择性实现其基类的抽象方法 abstract class BaseClass2 : BaseClass { } class DerivedClass : BaseClass { public override void AbstractMethod() { _x++; _y++; } public override int X // overriding property { get { return _x + 10; } } public override int Y // overriding property { get { return _y + 10; } } static void Main() { DerivedClass o = new DerivedClass(); o.AbstractMethod(); Console.WriteLine("x = {0}, y = {1}", o.X, o.Y); } }
相关文章推荐
- java 抽象类和接口有什么区别
- 什么是抽象类?什么是接口?两者有什么区别?
- 接口和抽象类有什么区别?你选择使用接口和抽象类的依据是什么?
- 接口和抽象类有什么区别
- 接口,抽象类的区别?abstractclass和integerface有什么区别?
- 接口和抽象类有什么区别 (转)
- 接口和抽象类有什么区别
- 接口和抽象类有什么区别
- 接口和抽象类有什么区别
- 接口和抽象类有什么区别?
- 接口和抽象类有什么区别
- 4.2.8 抽象类(abstract class)与接口(interface)有什么异同【Part3:区别】
- 接口和抽象类的区别是什么?
- 接口和抽象类有什么区别
- 基本概念的澄清 1。抽象类与接口在设计时各有什么好处? 2。静态方法与实例方法的区别(论坛帖抽取)
- 接口和抽象类有什么区别
- 接口和抽象类有什么区别
- 接口和抽象类有什么区别
- Java接口和抽象类有什么区别,什么时候用接口好,什么时候用抽象类好?
- 接口和抽象类有什么区别