定義
abstract關(guān)鍵字校哎,表明某個類只能是其他類的基類扶歪。
可以在父類中定義一個函數(shù),但是不去實現(xiàn)撩匕。所有繼承自該類的子類都必須實現(xiàn)該類中的所有抽象函數(shù)害捕。
功能:abstract關(guān)鍵字可以如下功能一起使用:類绿淋、屬性、方法尝盼、索引器及事件等吞滞。與接口比較,它是單繼承的盾沫,接口是多實現(xiàn)的裁赠。
注意:不可以用抽象類來進(jìn)行實例化,但可以用抽象類來聲明赴精。但凡某一個類中有一個抽象方法佩捞,那么該類也就必須定義為抽象類。
示例
在此例中蕾哟,類 Square 必須提供 Area 的實現(xiàn)一忱,因為它派生自 ShapesClass
abstract class ShapesClass
{
abstract public int Area();
}
class Square : ShapesClass
{
int side = 0;
public Square(int n)
{
side = n;
}
// Area method is required to avoid
// a compile-time error.
public override int Area()
{
return side * side;
}
static void Main()
{
Square sq = new Square(12);
Console.WriteLine("Area of the square = {0}", sq.Area());
}
interface I
{
void M();
}
abstract class C : I
{
public abstract void M();
}
}
// Output: Area of the square = 144