鍍金池/ 教程/ C#/ 繼承
循環(huán)
正則表達(dá)式
概述
委托
多態(tài)性
字符串
繼承
結(jié)構(gòu)體
集合
變量
不安全代碼
判斷
反射
異常處理
可空類型
方法
數(shù)據(jù)類型
命名空間
文件 I/O
類型轉(zhuǎn)換
屬性
程序結(jié)構(gòu)
事件
接口
預(yù)處理指令
運(yùn)算符
多線程
匿名方法
索引器
泛型
封裝
常量和文字
基本語法
特性
數(shù)組
環(huán)境配置
運(yùn)算符重載
枚舉

繼承

面向?qū)ο蟪绦蛟O(shè)計(jì)中最重要的一個(gè)概念就是繼承(inheritance)。繼承允許我們?cè)诹硪粋€(gè)類中定義一個(gè)新的類,這使得它更容易創(chuàng)建和維護(hù)一個(gè)應(yīng)用程序。這也提供了一個(gè)機(jī)會(huì)來重用代碼的功能,加快實(shí)現(xiàn)時(shí)間。

創(chuàng)建一個(gè)類的時(shí)候,不是要寫全新的數(shù)據(jù)成員和成員函數(shù),程序員可以指定新的類繼承一個(gè)已經(jīng)存在的類的成員。已有的類稱為基類(base class),新的類稱為派生類(derived class)。

繼承的思想實(shí)現(xiàn)了 IS-A 的關(guān)系。例如,哺乳動(dòng)物是(IS-A)動(dòng)物,狗是(IS-A)哺乳動(dòng)物,因此狗是(IS-A)一個(gè)動(dòng)物等。

基類和派生類

一個(gè)類可以從多個(gè)類或接口被派生,這意味著它可以從多個(gè)基類或接口繼承數(shù)據(jù)和函數(shù)。

用 C# 創(chuàng)建派生類的語法如下:

<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
   {
      public int getArea()
      { 
         return (width * height); 
      }
   }

   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();

         Rect.setWidth(5);
         Rect.setHeight(7);

         // 打印對(duì)象的面積
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Total area: 35

初始化基類

派生類繼承基類的成員變量和成員方法。因此,父類對(duì)象應(yīng)該是先于子類被創(chuàng)建。你可以在初始化列表中說明父類的初始化。

下面的程序論證了上述方法:

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("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }// Rectangle 類結(jié)束  

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

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5

C# 中的多重繼承

C# 不支持多重繼承。但是你可以使用接口來實(shí)現(xiàn)多重繼承。下面的程序?qū)崿F(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;
   }

   // 基類 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();

         //打印對(duì)象面積
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Total area: 35
Total paint cost: $2450
上一篇:變量下一篇:反射