c# 繼承
繼承是面向?qū)ο蟪绦蛟O(shè)計(jì)中最重要的概念之一。繼承允許我們根據(jù)一個(gè)類來定義另一個(gè)類,這使得創(chuàng)建和維護(hù)應(yīng)用程序變得更容易。同時(shí)也有利于重用代碼和節(jié)省開發(fā)時(shí)間。
當(dāng)創(chuàng)建一個(gè)類時(shí),程序員不需要完全重新編寫新的數(shù)據(jù)成員和成員函數(shù),只需要設(shè)計(jì)一個(gè)新的類,繼承了已有的類的成員即可。這個(gè)已有的類被稱為的基類,這個(gè)新的類被稱為派生類。
繼承的思想實(shí)現(xiàn)了 屬于(is-a) 關(guān)系。例如,哺乳動物 屬于(is-a) 動物,狗 屬于(is-a) 哺乳動物,因此狗 屬于(is-a) 動物。
1. 基類和派生類
一個(gè)類可以派生自多個(gè)類或接口,這意味著它可以從多個(gè)基類或接口繼承數(shù)據(jù)和函數(shù)。
c# 中創(chuàng)建派生類的語法如下:
<訪問修飾符符> class <基類>
{
?...
}
class <派生類> : <基類>
{
?...
}
假設(shè),有一個(gè)基類 shape,它的派生類是 rectangle:
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;
? ?}
? ?// 派生類
? ?class rectangle: shape
? ?{
? ? ? public int getarea()
? ? ? {
? ? ? ? ?return (width * height);
? ? ? }
? ?}
? ?
? ?class rectangletester
? ?{
? ? ? static void main(string[] args)
? ? ? {
? ? ? ? ?rectangle rect = new rectangle();
? ? ? ? ?rect.setwidth(5);
? ? ? ? ?rect.setheight(7);
? ? ? ? ?// 打印對象的面積
? ? ? ? ?console.writeline("總面積: {0}", ?rect.getarea());
? ? ? ? ?console.readkey();
? ? ? }
? ?}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會產(chǎn)生下列結(jié)果:
總面積: 35
2. 基類的初始化
派生類繼承了基類的成員變量和成員方法。因此父類對象應(yīng)在子類對象創(chuàng)建之前被創(chuàng)建。您可以在成員初始化列表中進(jìn)行父類的初始化。
下面的程序演示了這點(diǎn):
using system;
namespace rectangleapplication
{
? ?class rectangle
? ?{
? ? ? // 成員變量
? ? ? 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("長度: {0}", length);
? ? ? ? ?console.writeline("寬度: {0}", width);
? ? ? ? ?console.writeline("面積: {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("成本: {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í),它會產(chǎn)生下列結(jié)果:
長度: 4.5 寬度: 7.5 面積: 33.75 成本: 2362.5
3. c# 多重繼承
多重繼承指的是一個(gè)類別可以同時(shí)從多于一個(gè)父類繼承行為與特征的功能。與單一繼承相對,單一繼承指一個(gè)類別只可以繼承自一個(gè)父類。
c# 不支持多重繼承。但是,您可以使用接口來實(shí)現(xiàn)多重繼承。下面的程序演示了這點(diǎ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;
? ?}
? ?// 基類 paintcost
? ?public interface paintcost
? ?{
? ? ? int getcost(int area);
? ?}
? ?// 派生類
? ?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();
? ? ? ? ?// 打印對象的面積
? ? ? ? ?console.writeline("總面積: {0}", ?rect.getarea());
? ? ? ? ?console.writeline("油漆總成本: ${0}" , rect.getcost(area));
? ? ? ? ?console.readkey();
? ? ? }
? ?}
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會產(chǎn)生下列結(jié)果:
總面積: 35 油漆總成本: $2450


