面向?qū)ο缶幊讨凶钪匾母拍钪皇抢^承。繼承允許根據(jù)另一個(gè)類定義一個(gè)類,這樣可以更容易地創(chuàng)建和維護(hù)應(yīng)用程序。這也提供了重用代碼并加快了代碼的實(shí)現(xiàn)。
當(dāng)創(chuàng)建一個(gè)類時(shí),程序員可以指定這個(gè)新類應(yīng)該繼承現(xiàn)有類的哪些成員,而不是編寫繼承全部的數(shù)據(jù)成員和成員函數(shù)。這個(gè)現(xiàn)有的類稱為基類,新類稱為派生類或子類。
繼承的想法實(shí)現(xiàn)了IS-A(是一個(gè)什么類?)的關(guān)系。 例如,哺乳動(dòng)物IS是動(dòng)物,狗IS-A哺乳動(dòng)物,因此也是狗IS-A動(dòng)物,等等。
類可以從多個(gè)類或接口派生,它可以從多個(gè)基類或接口繼承數(shù)據(jù)和函數(shù)。
C# 中創(chuàng)建派生類,可使用如下語(yǔ)法:
<acess-specifier> class <base_class>
{
...
}
class <derived_class> : <base_class>
{
...
}
考慮有一個(gè)基類Shape及其派生類Rectangle,具體繼承的實(shí)現(xiàn)詳細(xì)如下:
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Derived class
class Rectangle: Shape
{
public int getArea()
{
return (width * height);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
Rect.setWidth(10);
Rect.setHeight(20);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:
Total area: 200
派生類繼承基類成員變量和成員方法。 因此,在創(chuàng)建子類之前應(yīng)該創(chuàng)建超類對(duì)象。您可以在成員初始化列表中給出超類初始化的說(shuō)明或具體實(shí)現(xiàn)。
以下程序演示如何實(shí)現(xiàn):
using System;
namespace RectangleApplication
{
class Rectangle
{
//member variables
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle
{
private double cost;
public Tabletop(double l, double w) : base(l, w)
{ }
public double GetCost()
{
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display()
{
base.Display();
Console.WriteLine("Cost: {0}", GetCost());
}
}
class ExecuteRectangle
{
static void Main(string[] args)
{
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:
Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5
C# 不支持多重繼承。但是,可以使用接口來(lái)實(shí)現(xiàn)多重繼承。下面示例程序?qū)⒀菔救绾螌?shí)現(xiàn):
using System;
namespace InheritanceApplication
{
class Shape
{
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
protected int width;
protected int height;
}
// Base class PaintCost
public interface PaintCost
{
int getCost(int area);
}
// Derived class
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果: