深入分析c# 繼承
繼承是面向?qū)ο蟪绦蛟O(shè)計中最重要的概念之一。繼承允許我們根據(jù)一個類來定義另一個類,這使得創(chuàng)建和維護(hù)應(yīng)用程序變得更容易。同時也有利于重用代碼和節(jié)省開發(fā)時間。
當(dāng)創(chuàng)建一個類時,程序員不需要完全重新編寫新的數(shù)據(jù)成員和成員函數(shù),只需要設(shè)計一個新的類,繼承了已有的類的成員即可。這個已有的類被稱為的基類,這個新的類被稱為派生類。
繼承的思想實(shí)現(xiàn)了 屬于(IS-A) 關(guān)系。例如,哺乳動物 屬于(IS-A) 動物,狗 屬于(IS-A) 哺乳動物,因此狗 屬于(IS-A) 動物。
基類和派生類
一個類可以派生自多個類或接口,這意味著它可以從多個基類或接口繼承數(shù)據(jù)和函數(shù)。
C# 中創(chuàng)建派生類的語法如下:
<訪問修飾符符> class <基類> { ... } class <派生類> : <基類> { ... }
假設(shè),有一個基類 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í)行時,它會產(chǎn)生下列結(jié)果:
總面積: 35
基類的初始化
派生類繼承了基類的成員變量和成員方法。因此父類對象應(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í)行時,它會產(chǎn)生下列結(jié)果:
長度: 4.5
寬度: 7.5
面積: 33.75
成本: 2362.5
C# 多重繼承
多重繼承指的是一個類別可以同時從多于一個父類繼承行為與特征的功能。與單一繼承相對,單一繼承指一個類別只可以繼承自一個父類。
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í)行時,它會產(chǎn)生下列結(jié)果:
總面積: 35
油漆總成本: $2450
以上就是深入分析c# 繼承的詳細(xì)內(nèi)容,更多關(guān)于c# 繼承的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog
這篇文章介紹了C#開發(fā)Windows UWP系列之對話框MessageDialog和ContentDialog,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C#實(shí)現(xiàn)IP代理池調(diào)度的示例代碼
這篇文章主要為大家介紹了C#實(shí)現(xiàn)IP代理池調(diào)度的相關(guān)知識,文中的示例代碼講解詳細(xì),具有一定的參考與學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2023-07-07C#設(shè)計模式之Template模板方法模式實(shí)現(xiàn)ASP.NET自定義控件 密碼強(qiáng)度檢測功能
這篇文章主要介紹了C#設(shè)計模式之Template模板方法模式實(shí)現(xiàn)ASP.NET自定義控件 密碼強(qiáng)度檢測功能,簡單介紹了模板方法模式的定義、原理及檢測密碼強(qiáng)度的相關(guān)使用技巧,需要的朋友可以參考下2017-09-09C#數(shù)值轉(zhuǎn)換-顯式數(shù)值轉(zhuǎn)換表(參考)
就是在將一種類型轉(zhuǎn)換成另外一種類型時,需要額外的代碼來完成這種轉(zhuǎn)換。2013-04-04C#零基礎(chǔ)開發(fā)中最重要的概念總結(jié)
這篇文章主要為大家詳細(xì)介紹了C#零基礎(chǔ)開發(fā)中最重要的一些概念,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,需要的可以參考一下2023-02-02C#中比較常用的DateTime結(jié)構(gòu)的使用方法
這篇文章主要介紹了C#中比較常用的DateTime結(jié)構(gòu)的使用方法,需要的朋友可以參考下2015-11-11C#使用CEFSharp獲取動態(tài)網(wǎng)頁源碼的演示步驟
CEFSharp是一個用C#編寫的庫,它是Chromium Embedded Framework (CEF) 的.NET封裝和擴(kuò)展,CEF允許開發(fā)者在自己的應(yīng)用程序中嵌入一個功能強(qiáng)大的HTML渲染引擎,從而能夠呈現(xiàn)網(wǎng)頁內(nèi)容,本文介紹了C#如何使用CEFSharp獲取動態(tài)網(wǎng)頁源碼,需要的朋友可以參考下2024-08-08