您的位置:首页 > 编程语言 > C#

C#接口与抽象类的区别

2009-09-17 20:55 239 查看
接口和抽象类的区别是很多人比较容易搞混的问题,自己以前也没有很好的总结过,但是最近读到一本书叫
Programming .net Components

里面谈到了二者的区别,而且做了很好的总结。这本书有中文版的,但是不知道是翻译的原因还是原文就有问题,里面有些说的还是有些小问题。这里根据书中的总结再次加以整理:

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#
是不支持多继承的,如果需要多继承我们可以用接口实现(注意在一个类即实现接口又继承其他父类时,接口要在后面)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: