Interface和abstract class

这两个概念在C#和Java类似,从语法上看

  1. Interface和abstract class都不能实例化。

  2. 可以继承多个Interface,但不能继承多个abstract class。

  3. 继承Interface的子类必须实现Interface全部的方法,而abstract class子类可以选择是否实现基类的方法。

  4. Interface除包含方法外,还可以包含属性,索引器,事件,不应该包含常量,构造函数,析构函数,静态成员。abstract
    class则可以包含上述任意成员。

从设计上看,Interface是定义继承的方法,而abstract class定义继承的对象。

  1. Interface表达HAS-A的关系

  2. abstract class表达IS-A的关系

How about an analogy: when I was in the Air Force, I went to pilot training
and became a USAF pilot. At that point I wasn‘t qualified to fly anything, and
had to attend aircraft type training. Once I qualified, I was a pilot
(Abstract class) and a C-141 pilot (concrete class). At one of my assignments,
I was given an additional duty: Safety Officer. Now I was still a pilot and a
C-141 pilot, but I also performed Safety Officer duties (I implemented
ISafetyOfficer, so to speak). A pilot wasn‘t required to be a safety officer,
other people could have done it as well.

All USAF pilots have to follow certain Air Force-wide regulations, and all
C-141 (or F-16, or T-38) pilots ‘are‘ USAF pilots. Anyone can be a safety
officer. So, to summarize:

  • Pilot: abstract class

  • C-141 Pilot: concrete class

  • ISafety Officer: interface

added note: this was meant to be an analogy to help explain the concept,
not a coding recommendation. See the various comments below, the discussion is
interesting.  

reference:

http://kb.cnblogs.com/page/41836/

http://dev.yesky.com/436/7581936.shtml

http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo?lq=1

http://stackoverflow.com/questions/56867/interface-vs-base-class

时间: 2024-08-06 20:07:29

Interface和abstract class的相关文章

接口和抽象类:Interface、abstract _【转】

一.接口 接口是C#中很常见的工具,概念什么的就不说了,这里讲几个值得注意的小地方: 1.接口内部只能有函数.属性和事件的声明: interface IParent { void Show(); string Type { get; set; } event AddChildren Add; } 在接口中声明的成员都不需要访问修饰符(public,private等),因为接口成员的权限默认都是public,另外值得注意的是接口中之所以能够声明事件是因为事件就是委托的特殊属性. 接口不能是静态的,

interface vs abstract

[interface vs abstract] 1.interface中的方法不能用public.abstract修饰,interface中的方法只包括signature. 2.一个类只能继承一个abstract class,却可以实现多个interface. 3.abstract class表示的是"is a"关系,描述一类对象的特性. 4.interface 表示的是能力的关系“capable”,描述一类对象是否拥有某种能力(是否继承哪个类). 参考: 1.http://wenku

笔试题之interface和abstract class之间的区别

interface和abstract class之间有哪些区别?在C#中 1.首先abstract class 还是class,所以可以有数据成员,interface不可以 2.abstract class 可以有非abstract方法,而且可以有方法的实现,interface的方式不能有实现 3.abstract class 只能单继承,interface可以多继承. 4.interface只能使用public之类关键字

Interface VS Abstract Class

An abstract class can have shared state or functionality. An interface is only a promise to provide the state or functionality. A good abstract class will reduce the amount of code that has to be rewritten because it's functionality or state can be s

在C#中interface与abstract class的区别

1)在继承抽象类时,必须覆盖该类中的每一个抽象方法,而每个已实现的方法必须和抽象类中指定的方法一样,接收相同数目和类型的参数,具有同样的返回值,这一点与接口相同. 2)当父类已有实际功能的方法时,该方法在子类中可以不必实现,直接引用的方法,子类也可以重写该父类的方法(继承的概念). 3)而实现 (implement)一个接口(interface)的时候,是一定要实现接口中所定义的所有方法,而不可遗漏任何一个. 4)另外,抽象类不能产生对象的,但可以由它的实现类来声明对象. A. 两者都是抽象类,

Interface与abstract类的区别

含有abstract修饰符的class即为抽象类,abstract 类不能创建的实例对象.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不必是抽象的.abstract class类中定义抽象方法必须在具体(Concrete)子类中实现,所以,不能有抽象构造方法或抽象静态方法.如果的子类没有实现抽象父类中的所有抽象方法,那么子类也必须定义为abstract类型. 接口(interface)可以说成是抽象类的一种特例,接口中的所有方法都必须

Interface 和 abstract 了解

转自:https://blog.csdn.net/mandypan/article/details/52138062 在Java语言中,abstract class和interface是支持抽象类定义的两种机制. 不能创建abstract类的实例,然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例. 不能有抽象构造函数或抽象静态方法. Abstract 类的子类为它们父类中的所有抽象方法提供实现,否则它们也是抽象类. 接口(interface)是抽象类的变体. 在接口中,所有

JAVA interface & abstract

摘要:JAVA interface & abstract 界面 & 抽象类 抽象定义:抽象就是从多个事物中将共性的,本质的内容抽取出来. ex:?狼和狗共性都是犬科,犬科就是抽象出来的概念.鸟跟飞机都会飞,但飞的内容不同 抽象类:Java中可以定义没有方法体的方法,该方法的具体实现由子类完成,该方法称为抽象方法,包含抽象方法的类就是抽象类. abstract class Action{ ?????? abstract void fly(); } 抽象类特点: 1.??????????抽象方

C++虚函数virtual,纯虚函数pure virtual和Java抽象函数abstract,接口interface与抽象类abstract class的比较

由于C++和Java都是面向对象的编程语言,它们的多态性就分别靠虚函数和抽象函数来实现. C++的虚函数可以在子类中重写,调用是根据实际的对象来判别的,而不是通过指针类型(普通函数的调用是根据当前指针类型来判断的).纯虚函数是一种在父函数中只定义而不实现的一种函数,不能用来声明对象,也可以被称为抽象类.纯虚函数的实现也可以在类声明外进行定义.C++中的抽象类abstract class是指至少有一个纯虚函数的类,如果一个类全部由纯虚函数组成,不包括任何的实现,被称为纯虚类. Java中的普通函数