在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ C#繼承
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關鍵字
C# StreamReader類
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類
C#主線程
C#數(shù)據(jù)類型
C# FileStream類
C#預處理指令
C#繼承
C#循環(huán)
C#決策結(jié)構(gòu)
C#集合
C#反射
C#類型轉(zhuǎn)換
C#泛型
C# StringReader類
C#歷史
C#運算符重載
C#屬性
C#線程實例:Sleep()方法
C#線程示例:優(yōu)先級
C#線程實例:Join()方法
C# BinaryReader類
C#類
C#索引器
C# BinaryWriter類
C#序列化
C#常量和文字
C#程序結(jié)構(gòu)
C#封裝
C#事件
C#可空類型(nullable)
C#基本語法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實例
C# StringWriter類
C#線程實例
C#數(shù)組
C#正則表達式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類
C#線程實例:Abort()方法
C#結(jié)構(gòu)體
C#運算符
C#入門程序
C#多線程生命周期
C# TextReader類
C# DirectoryInfo類
C#委托

C#繼承

面向?qū)ο缶幊讨凶钪匾母拍钪皇抢^承。繼承允許根據(jù)另一個類定義一個類,這樣可以更容易地創(chuàng)建和維護應用程序。這也提供了重用代碼并加快了代碼的實現(xiàn)。

當創(chuàng)建一個類時,程序員可以指定這個新類應該繼承現(xiàn)有類的哪些成員,而不是編寫繼承全部的數(shù)據(jù)成員和成員函數(shù)。這個現(xiàn)有的類稱為基類,新類稱為派生類或子類。

繼承的想法實現(xiàn)了IS-A(是一個什么類?)的關系。 例如,哺乳動物IS是動物,狗IS-A哺乳動物,因此也是狗IS-A動物,等等。

基類和派生類

類可以從多個類或接口派生,它可以從多個基類或接口繼承數(shù)據(jù)和函數(shù)。

C# 中創(chuàng)建派生類,可使用如下語法:

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

考慮有一個基類Shape及其派生類Rectangle,具體繼承的實現(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;
   }

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

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果:

Total area: 200

初始化基類

派生類繼承基類成員變量和成員方法。 因此,在創(chuàng)建子類之前應該創(chuàng)建超類對象。您可以在成員初始化列表中給出超類初始化的說明或具體實現(xiàn)。

以下程序演示如何實現(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();
      }
   }
}

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果:

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

C# 多重繼承

C# 不支持多重繼承。但是,可以使用接口來實現(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;
   }

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

當編譯和執(zhí)行上述代碼時,會產(chǎn)生以下結(jié)果: