目錄
C# 面向對象的三大特性分別是封裝、繼承、多態,下面將幫助您更深一步瞭解並運用這三大特性。
一、封裝
封裝 被定義爲"把一個或多個項目封閉在一個物理的或者邏輯的包中"。在面向對象程序設計方法論中,封裝是爲了防止對實現細節的訪問。
抽象和封裝是面向對象程序設計的相關特性。抽象允許相關信息可視化,封裝則使開發者實現所需級別的抽象。
C# 封裝根據具體的需要,設置使用者的訪問權限,並通過 訪問修飾符 來實現。
一個 訪問修飾符 定義了一個類成員的範圍和可見性。C# 支持的訪問修飾符如下所示:
- public:所有對象都可以訪問;
- private:對象本身在對象內部可以訪問;
- protected:只有該類對象及其子類對象可以訪問
- internal:同一個程序集的對象可以訪問(即帶有 internal 訪問修飾符的任何成員可以被定義在該成員所定義的應用程序內的任何類或方法訪問);
- protected internal:訪問限於當前程序集或派生自包含類的類型。(訪問修飾符允許在本類,派生類或者包含該類的程序集中訪問。這也被用於實現繼承。)
internal示例:
using System;
namespace RectangleApplication
{
class Rectangle
{
//成員變量
internal double length;
internal double width;
double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("長度: {0}", length);
Console.WriteLine("寬度: {0}", width);
Console.WriteLine("面積: {0}", GetArea());
}
}//end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
二、繼承
繼承 是面向對象程序設計中最重要的概念之一。繼承允許我們根據一個類來定義另一個類,這使得創建和維護應用程序變得更容易。同時也有利於重用代碼和節省開發時間。
當創建一個類時,程序員不需要完全重新編寫新的數據成員和成員函數,只需要設計一個新的類,繼承了已有的類的成員即可。這個已有的類被稱爲的基類,這個新的類被稱爲派生類。
繼承的思想實現了 屬於(IS-A) 關係。例如,哺乳動物 屬於(IS-A) 動物,狗 屬於(IS-A) 哺乳動物,因此狗 屬於(IS-A) 動物。
基類和派生類
一個類可以派生自多個類或接口,這意味着它可以從多個基類或接口繼承數據和函數。
假設,有一個基類 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();
}
}
}
基類的初始化
派生類繼承了基類的成員變量和成員方法。因此父類對象應在子類對象創建之前被創建。您可以在成員初始化列表中進行父類的初始化。
示例如下:
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();
}
}
}
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();
}
}
}
三、多態
多態 是同一個行爲具有多個不同表現形式或形態的能力。
多態性意味着有多重形式。在面向對象編程範式中,多態性往往表現爲"一個接口,多個功能"。
多態性可以是靜態的或動態的。在靜態多態性中,函數的響應是在編譯時發生的。在動態多態性中,函數的響應是在運行時發生的。
在 C# 中,每個類型都是多態的,因爲包括用戶定義類型在內的所有類型都繼承自 Object。
多態就是同一個接口,使用不同的實例而執行不同操作。
現實中,比如我們按下 F1 鍵這個動作:
- 如果當前在 Flash 界面下彈出的就是 AS 3 的幫助文檔;
- 如果當前在 Word 下彈出的就是 Word 幫助;
- 在 Windows 下彈出的就是 Windows 幫助和支持。
同一個事件發生在不同的對象上會產生不同的結果。
1、靜態多態性
在編譯時,函數和對象的連接機制被稱爲早期綁定,也被稱爲靜態綁定。C# 提供了兩種技術來實現靜態多態性。分別爲:
- 函數重載
- 運算符重載
函數重載
您可以在同一個範圍內對相同的函數名有多個定義。函數的定義必須彼此不同,可以是參數列表中的參數類型不同,也可以是參數個數不同。不能重載只有返回類型不同的函數聲明。
運算符重載
您可以重定義或重載 C# 中內置的運算符。因此,程序員也可以使用用戶自定義類型的運算符。重載運算符是具有特殊名稱的函數,是通過關鍵字 operator 後跟運算符的符號來定義的。與其他函數一樣,重載運算符有返回類型和參數列表。
例如:
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
上面的函數爲用戶自定義的類 Box 實現了加法運算符(+)。它把兩個 Box 對象的屬性相加,並返回相加後的 Box 對象。
運算符重載的完整實現:
using System;
namespace OperatorOvlApplication
{
class Box
{
private double length; // 長度
private double breadth; // 寬度
private double height; // 高度
public double getVolume()
{
return length * breadth * height;
}
public void setLength( double len )
{
length = len;
}
public void setBreadth( double bre )
{
breadth = bre;
}
public void setHeight( double hei )
{
height = hei;
}
// 重載 + 運算符來把兩個 Box 對象相加
public static Box operator+ (Box b, Box c)
{
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
}
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // 聲明 Box1,類型爲 Box
Box Box2 = new Box(); // 聲明 Box2,類型爲 Box
Box Box3 = new Box(); // 聲明 Box3,類型爲 Box
double volume = 0.0; // 體積
// Box1 詳述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 詳述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Box1 的體積
volume = Box1.getVolume();
Console.WriteLine("Box1 的體積: {0}", volume);
// Box2 的體積
volume = Box2.getVolume();
Console.WriteLine("Box2 的體積: {0}", volume);
// 把兩個對象相加
Box3 = Box1 + Box2;
// Box3 的體積
volume = Box3.getVolume();
Console.WriteLine("Box3 的體積: {0}", volume);
Console.ReadKey();
}
}
}
可重載和不可重載運算符
下表描述了 C# 中運算符重載的能力:
運算符 | 描述 |
---|---|
+(正), -(負), !, ~(位非), ++, -- | 這些一元運算符只有一個操作數,且可以被重載。 |
+, -, *, /, % | 這些二元運算符帶有兩個操作數,且可以被重載。 |
==, !=, <, >, <=, >= | 這些比較運算符可以被重載。 |
&&, || | 這些條件邏輯運算符不能被直接重載。 |
+=, -=, *=, /=, %= | 這些賦值運算符不能被重載。 |
=, ., ?:, ->, new, is, sizeof, typeof | 這些運算符不能被重載。 |
2、動態多態性
C# 允許使用關鍵字 abstract 創建抽象類,用於提供接口的部分類的實現。當一個派生類繼承自該抽象類時,實現即完成。抽象類包含抽象方法,抽象方法可被派生類實現。派生類具有更專業的功能。
請注意,下面是有關抽象類的一些規則:
- 不能創建一個抽象類的實例。
- 不能在一個抽象類外部聲明一個抽象方法。
- 通過在類定義前面放置關鍵字 sealed,可以將類聲明爲密封類。當一個類被聲明爲 sealed 時,它不能被繼承。抽象類不能被聲明爲 sealed。
抽象類示例:
using System;
namespace PolymorphismApplication
{
abstract class Shape
{
abstract public int area();
}
class Rectangle: Shape
{
private int length;
private int width;
public Rectangle( int a=0, int b=0)
{
length = a;
width = b;
}
public override int area ()
{
Console.WriteLine("Rectangle 類的面積:");
return (width * length);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("面積: {0}",a);
Console.ReadKey();
}
}
}
當有一個定義在類中的函數需要在繼承類中實現時,可以使用虛方法。
- 虛方法是使用關鍵字 virtual 聲明的。
- 虛方法可以在不同的繼承類中有不同的實現。
- 對虛方法的調用是在運行時發生的。
- 動態多態性是通過 抽象類 和 虛方法 實現的。
以下實例創建了 Shape 基類,並創建派生類 Circle、 Rectangle、Triangle, Shape 類提供一個名爲 Draw 的虛擬方法,在每個派生類中重寫該方法以繪製該類的指定形狀。
示例:
using System;
using System.Collections.Generic;
public class Shape
{
public int X { get; private set; }
public int Y { get; private set; }
public int Height { get; set; }
public int Width { get; set; }
// 虛方法
public virtual void Draw()
{
Console.WriteLine("執行基類的畫圖任務");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("畫一個圓形");
base.Draw();
}
}
class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("畫一個長方形");
base.Draw();
}
}
class Triangle : Shape
{
public override void Draw()
{
Console.WriteLine("畫一個三角形");
base.Draw();
}
}
class Program
{
static void Main(string[] args)
{
// 創建一個 List<Shape> 對象,並向該對象添加 Circle、Triangle 和 Rectangle
var shapes = new List<Shape>
{
new Rectangle(),
new Triangle(),
new Circle()
};
// 使用 foreach 循環對該列表的派生類進行循環訪問,並對其中的每個 Shape 對象調用 Draw 方法
foreach (var shape in shapes)
{
shape.Draw();
}
Console.WriteLine("按下任意鍵退出。");
Console.ReadKey();
}
}
下面的程序演示通過虛方法 area() 來計算不同形狀圖像的面積:
using System;
namespace PolymorphismApplication
{
class Shape
{
protected int width, height;
public Shape( int a=0, int b=0)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("父類的面積:");
return 0;
}
}
class Rectangle: Shape
{
public Rectangle( int a=0, int b=0): base(a, b)
{
}
public override int area ()
{
Console.WriteLine("Rectangle 類的面積:");
return (width * height);
}
}
class Triangle: Shape
{
public Triangle(int a = 0, int b = 0): base(a, b)
{
}
public override int area()
{
Console.WriteLine("Triangle 類的面積:");
return (width * height / 2);
}
}
class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("面積: {0}", a);
}
}
class Tester
{
static void Main(string[] args)
{
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}