[TOC]
```
<訪問標識符,默認internal> class ?類名><:基類>
{
? ? // 成員變量
? ? <訪問標識符,默認private> <變量數據類型> variable1;
? ? <訪問標識符,默認private> <變量數據類型> variable2;
? ? ...
? ? <訪問標識符,默認private> <變量數據類型> variableN;
? ? // 成員方法
? ? <訪問標識符,默認private> <返回類型> method1(parameter_list)
? ? {
? ? ? ? // method body
? ? }
? ? <訪問標識符,默認private> <返回類型> method2(parameter_list)
? ? {
? ? ? ? // method body
? ? }
? ? ...
? ? <訪問標識符,默認private> <返回類型> methodN(parameter_list)
? ? {
? ? ? ? // method body
? ? }
}
```
```
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();
? ? ? }
? ?}
}
```
# **封裝**
* public:所有對象都可以訪問;
* private:對象本身在對象內部可以訪問;
* protected:只有該類對象及其子類對象可以訪問
* internal:同一個程序集的對象可以訪問;
* protected internal:訪問限于當前程序集或派生自包含類的類型。

**Internal 訪問修飾符**
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();
? ? ? ? }
? ? }
}
```
**Protected Internal 訪問修飾符**
Protected Internal 訪問修飾符允許在本類,派生類或者包含該類的程序集中訪問。這也被用于實現繼承。
# **繼承:`class 派生類:基類`**
# **多態**
## **靜態多態性**
### **函數重載**
函數名相同,參數不同或者參數類型不同
```
using System;
namespace PolymorphismApplication
{
? ? public class TestData ?
? ? { ?
? ? ? ? public int Add(int a, int b, int c) ?
? ? ? ? { ?
? ? ? ? ? ? return a + b + c; ?
? ? ? ? }
?
? ? ? ? public int Add(int a, int b) ?
? ? ? ? { ?
? ? ? ? ? ? return a + b; ?
? ? ? ? }
void print(int i)
{
Console.WriteLine("輸出整型: {0}", i );
}
void print(double f)
{
Console.WriteLine("輸出浮點型: {0}" , f);
}
void print(string s)
{
Console.WriteLine("輸出字符串: {0}", s);
}
?
? ? }
? ? class Program ?
? ? { ?
? ? ? ? static void Main(string[] args) ?
? ? ? ? { ?
? ? ? ? ? ? TestData t = new TestData();
? ? ? ? ? ? int add1 = t.Add(1, 2);
? ? ? ? ? ? int add2 = t.Add(1, 2, 3);
? ? ? ? ? ? Console.WriteLine("add1 :" + add1);//add1 :3
? ? ? ? ? ? Console.WriteLine("add2 :" + add2);//add2 :6
//調用print
t.print(1);//輸出整型: 1
t.print(1.23);//輸出浮點型: 1.23
t.print("Hello Runoob");//輸出字符串: Hello Runoob
Console.ReadKey();
? ? ? ? } ?
? ? }
?
}
```
### **運算符重載:operator運算符**
重載運算符是具有特殊名稱的函數,是通過關鍵字**operator**后跟運算符的符號來定義的。與其他函數一樣,重載運算符有返回類型和參數列表
函數為用戶自定義的類 Box 實現了加法運算符(+)。它把兩個 Box 對象的屬性相加,并返回相加后的 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;
}
```
運算符重載
```
using System;
namespace OperatorOvlApplication
{
class Box
{
private double chang; // 長度
private double kuan; // 寬度
private double gao; // 高度
public double getVolume()
{
return chang * kuan * gao;
}
public void setChang( double len )
{
chang = len;
}
public void setKuan( double bre )
{
kuan = bre;
}
public void setGao( double hei )
{
gao = hei;
}
// 重載 + 運算符來把兩個 Box 對象相加
public static Box operator+ (Box b1, Box b2)
{
Box box = new Box();
box.chang = b1.chang + b2.chang;
box.kuan = b1.kuan + b2.kuan;
box.gao = b1.gao + b2.gao;
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.setChang(6.0);
Box1.setKuan(7.0);
Box1.setGao(5.0);
// Box2 詳述
Box2.setChang(12.0);
Box2.setKuan(13.0);
Box2.setGao(10.0);
// Box1 的體積
volume = Box1.getVolume();//5.0 * 6.0 * 7.0=210
Console.WriteLine("Box1 的體積: {0}", volume);
// Box2 的體積
volume = Box2.getVolume();//12 * 13 * 10 = 1560
Console.WriteLine("Box2 的體積: {0}", volume);
// 把兩個對象相加
Box3 = Box1 + Box2;
// Box3 的體積
volume = Box3.getVolume();// (6+12) * (7+13) * (5+10) = 18 * 20 * 15= 5400
Console.WriteLine("Box3 的體積: {0}", volume);
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();
? ? ? }
? ?}
}
```
結果:
~~~
Rectangle 類的面積:
面積:70
Triangle 類的面積:
面積:25
~~~
- Visual Studio 2022安裝到非C盤
- .net平臺區別
- 常用單詞
- 關鍵字
- 操作符(運算符)
- 標識符(命名規范)
- 開始
- 變量
- 常量
- 數據類型
- 值類型
- 變量數據類型
- 枚舉類型enum(常量集合)
- 結構類型struct(結構體)
- 元組類型
- 可null類型(T?)
- 引用類型
- 數組(array)
- 集合(List)
- 內置引用類型
- object
- string
- Dynamic(動態類型)
- delegate委托(代理)類型
- 自定義引用類型
- 接口(interface)
- 類class
- record(定義一個引用類型)
- 指針類型(僅用于非安全代碼)
- get和set訪問器
- delegate委托
- delegate實現發布訂閱與事件
- 類型轉換
- 合并操作符??
- 類相關
- Partial 部分類
- 類定義以及訪問修飾符(封裝)
- abstract抽象類與sealed密封類
- virtual虛方法
- 接口interface
- C# 預處理器指令
- C#技術棧
- 判斷(流程控制)與三元運算
- if
- switch
- 三元運算
- 循環
- while 循環
- for循環
- foreach循環
- do...while 循環
- 文件操作
- 其他
- 多開