C# 繼承

來源:互聯網
上載者:User
繼承是物件導向程式設計中最重要的概念之一。繼承允許我們根據一個類來定義另一個類來定義一個類,這使得建立和維護應用程式變得更容易。同時也有利於重用代碼和節省開發時間。

當建立一個類時,程式員不需要完全重新編寫新的資料成員和成員函數,只需要設計一個新的類,繼承了已有的類的成員即可。這個已有的類被稱為的基類,這個新的類被稱為衍生類別。

繼承的思想實現了 屬於(IS-A) 關係。例如,哺乳動物 屬於(IS-A) 動物,狗 屬於(IS-A) 哺乳動物,因此狗 屬於(IS-A) 動物。

基類和衍生類別

一個類可以派生自多個類或介面,這意味著它可以從多個基類或介面繼承資料和函數。

C# 中建立衍生類別的文法如下:

<acess-specifier> class <base_class>{ ... } class <derived_class> : <base_class>{ ... }

假設,有一個基類 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 { publicint getArea() { return (width * height); } } class RectangleTester { static voidMain(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // 列印對象的面積 Console.WriteLine("總面積: {0}", Rect.getArea()); Console.ReadKey(); } }}

當上面的代碼被編譯和執行時,它會產生下列結果:

總面積: 35

基類的初始化

衍生類別繼承了基類的成員變數和成員方法。因此父類對象應在子類對象建立之前被建立。您可以在成員初始化列表中進行父類的初始化。

下面的程式示範了這點:

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; publicTabletop(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()); } } classExecuteRectangle { static void Main(string[] args) { Tabletop t = new Tabletop(4.5,7.5); t.Display(); Console.ReadLine(); } }}

當上面的代碼被編譯和執行時,它會產生下列結果:

長度: 4.5寬度: 7.5面積: 33.75成本: 2362.5

C# 多重繼承

多重繼承指的是一個類別可以同時從多於一個父類繼承行為與特徵的功能。與單一繼承相對,單一繼承指一個類別只可以繼承自一個父類。

C# 不支援多重繼承。但是,您可以使用介面來實現多重繼承。下面的程式示範了這點:

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(); } }}

當上面的代碼被編譯和執行時,它會產生下列結果:

總面積: 35油漆總成本: $2450

更多C# 繼承相關文章請關注topic.alibabacloud.com!
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.