# C# 繼承
繼承是面向對象程序設計中最重要的概念之一。繼承允許我們根據一個類來定義另一個類來定義一個類,這使得創建和維護應用程序變得更容易。同時也有利于重用代碼和節省開發時間。
當創建一個類時,程序員不需要完全重新編寫新的數據成員和成員函數,只需要設計一個新的類,繼承了已有的類的成員即可。這個已有的類被稱為的**基類**,這個新的類被稱為**派生類**。
繼承的思想實現了 **屬于(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
{
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();
}
}
}
```
當上面的代碼被編譯和執行時,它會產生下列結果:
```
總面積: 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;
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();
}
}
}
```
當上面的代碼被編譯和執行時,它會產生下列結果:
```
長度: 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# 基礎
- C# 簡介
- C# 環境
- C# 程序結構
- C# 基本語法
- C# 數據類型
- C# 類型轉換
- C# 變量
- C# 常量
- C# 運算符
- C# 判斷
- C# 循環
- C# 封裝
- C# 方法
- C# 可空類型(Nullable)
- C# 數組(Array)
- C# 字符串(String)
- C# 結構(Struct)
- C# 枚舉(Enum)
- C# 類(Class)
- C# 繼承
- C# 多態性
- C# 運算符重載
- C# 接口(Interface)
- C# 命名空間(Namespace)
- C# 預處理器指令
- C# 正則表達式
- C# 異常處理
- C# 文件的輸入與輸出
- C# 高級
- C# 特性(Attribute)
- C# 反射(Reflection)
- C# 屬性(Property)
- C# 索引器(Indexer)
- C# 委托(Delegate)
- C# 事件(Event)
- C# 集合(Collection)
- C# 泛型(Generic)
- C# 匿名方法
- C# 不安全代碼
- C# 多線程
- ASP.NET 簡介
- Web Pages 教程
- ASP.NET Web Pages - 教程
- ASP.NET Web Pages - 添加 Razor 代碼
- ASP.NET Web Pages - 頁面布局
- ASP.NET Web Pages - 文件夾
- ASP.NET Web Pages - 全局頁面
- ASP.NET Web Pages - HTML 表單
- ASP.NET Web Pages - 對象
- ASP.NET Web Pages - 文件
- ASP.NET Web Pages - 幫助器
- ASP.NET Web Pages - WebGrid 幫助器
- ASP.NET Web Pages - Chart 幫助器
- ASP.NET Web Pages - WebMail 幫助器
- ASP.NET Web Pages - PHP
- ASP.NET Web Pages - 發布網站
- Razor 教程
- ASP.NET Razor - 標記
- ASP.NET Razor - C# 和 VB 代碼語法
- ASP.NET Razor - C# 變量
- ASP.NET Razor - C# 循環和數組
- ASP.NET Razor - C# 邏輯條件
- ASP.NET Razor - VB 變量
- ASP.NET Razor - VB 循環和數組
- ASP.NET Razor - VB 邏輯條件
- MVC 教程
- ASP.NET MVC 教程
- ASP.NET MVC - Internet 應用程序
- ASP.NET MVC - 應用程序文件夾
- ASP.NET MVC - 樣式和布局
- ASP.NET MVC - 控制器
- ASP.NET MVC - 視圖
- ASP.NET MVC - SQL 數據庫
- ASP.NET MVC - 模型
- ASP.NET MVC - 安全
- ASP.NET MVC - HTML 幫助器
- ASP.NET MVC - 發布網站
- Web Forms 教程
- ASP.NET Web Forms - 教程
- ASP.NET Web Forms - HTML 頁面
- ASP.NET Web Forms - 服務器控件
- ASP.NET Web Forms - 事件
- ASP.NET Web Forms - HTML 表單
- ASP.NET Web Forms - 維持 ViewState
- ASP.NET Web Forms - TextBox 控件
- ASP.NET Web Forms - Button 控件
- ASP.NET Web Forms - 數據綁定
- ASP.NET Web Forms - ArrayList 對象
- ASP.NET Web Forms - Hashtable 對象
- ASP.NET Web Forms - SortedList 對象
- ASP.NET Web Forms - XML 文件
- ASP.NET Web Forms - Repeater 控件
- ASP.NET Web Forms - DataList 控件
- ASP.NET Web Forms - 數據庫連接
- ASP.NET Web Forms - 母版頁
- ASP.NET Web Forms - 導航
- Web Pages 參考手冊
- ASP.NET Web Pages - 類
- ASP.NET Web Pages - WebSecurity 對象
- ASP.NET Web Pages - Database 對象
- ASP.NET Web Pages - WebMail 對象
- ASP.NET Web Pages - 更多幫助器
- MVC - 參考手冊
- Web Forms 參考手冊
- ASP.NET Web Forms - HTML 服務器控件
- ASP.NET Web Forms - Web 服務器控件
- ASP.NET Web Forms - Validation 服務器控件
- 免責聲明