the Differences between abstract class interface in C#接口和抽象类的区别
????abstract class and interface in C# look like much, but sometimes they get the similiar use. However, there are still much differences between them. here we go.
????抽象類和接口初看上去很象,但是還是有很多區(qū)別的。
?? a) Interface Grammar接口語法
?? [public|protected|internal|private] interface interfaceName
????{
????????public int add(int x,int y);
//this?is?error! interface memeber can not be described with any?characters?to?declare its access attribute.
//接口中任何成員和屬性不能被任何修飾符修飾來表明其訪問屬性!
????????int add(int x,int y);
//this is right!
????????int Count{get;} //interface property接口屬性
????????string this[int index]{get;set;}//index interface?接口索引
????}
????Difference:????
????????1. in interface, all interface members can not be?decorated with any?other access tag,such as public,private.etc.
????????2. in?abtract class,?such case is just on the opposite.
????
????b) abstract class Grammar 抽象類語法
????????[public|protected|internal|private] abstct class className
????????{
????????????public abstract int?add(int x,int y);
????????????//it's ok, but that forbidden in?interface!
????????????//在abstract class中,類成員可以有各種修飾符。如果該類成員沒有被implement,那么該類一定要被標示為abstact方法。否則出錯!當然也可以實現(xiàn)該方法。如下multiply方法所示
????????????public virtual int?multiply(int x,int y)
????????????{
????????????????return x*y;
????????????}
????????}
????????Difference:
????????1.in interface,?all interface memebers can not be implemented. and it's just an declaration? for?other class?which will inherented from it and?implemented all of them.
????????在接口中,所有接口成員都不能被實現(xiàn),它只是一個申明而已,其他類繼承自該接口,并且要實現(xiàn)該接口中的所有成員。
????????2 in abstract class, its member can be implemented?,otherwise must be decorated with abstract that is the same with interface .?of course,?any class inherented from this? must implemented all?its abstract method.?of course,?u can?also override the method?that?has already implemented method.
????????在抽象類中,其成員可以被實現(xiàn),如果不實現(xiàn)的話,其方法成員一定要被標標示為abstract.?同時,任何繼承于該抽象類的類必須要實現(xiàn)其所有標示為abstract的方法,這點與interface相同。或者該類也可以override該抽象類中的method.?這一點interface絕對不可能,因為interface中根本沒有實現(xiàn)過,何來override?
????????
轉載于:https://www.cnblogs.com/Winston/archive/2008/04/23/1166876.html
總結
以上是生活随笔為你收集整理的the Differences between abstract class interface in C#接口和抽象类的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 88 岁 MIT 数学泰斗最后一课,任教
- 下一篇: 华晨中国:正式建议出资华晨雷诺金杯汽车有