溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

深入淺出OOP(六): 理解C#的Enums

發(fā)布時(shí)間:2020-07-12 04:10:15 來(lái)源:網(wǎng)絡(luò) 閱讀:297 作者:powertoolsteam 欄目:編程語(yǔ)言

MSDN定義:枚舉類型(也稱為枚舉)為定義一組可以賦給變量的命名整數(shù)常量提供了一種有效的方法。  例如,假設(shè)您必須定義一個(gè)變量,該變量的值表示一周中的一天。

該變量只能存儲(chǔ)七個(gè)有意義的值。 若要定義這些值,可以使用枚舉類型。枚舉類型是使用 enum 關(guān)鍵字聲明的。

深入淺出OOP(六): 理解C#的Enums

 從OOP上來(lái)說(shuō),枚舉的角色和和class一樣,它創(chuàng)建了一種新的數(shù)據(jù)類型。

  1: namespace Enums
  2: {
  3:     class Program
  4:     {
  5:         static void Main(string[] args)
  6:         {
  7:         }
  8:     }
  9:
 10:     enum Color
 11:     {
 12:         Yellow,
 13:         Blue,
 14:         Brown,
 15:         Green
 16:     }
 17: }

上面的代碼,我們使用enum的關(guān)鍵字,創(chuàng)建了新的數(shù)據(jù)類型Color,并包含4個(gè)值:YellowBlueBrown和Green。下面的例子我們給予Color枚舉。

 

直接輸出枚舉,則可得到枚舉的字符

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine(Color.Yellow);
  9:             Console.ReadLine();
 10:         }
 11:     }
 12:
 13:     enum Color
 14:     {
 15:         Yellow,
 16:         Blue,
 17:         Brown,
 18:         Green
 19:     }
 20: }

運(yùn)行程序,輸出:

Yellow

強(qiáng)轉(zhuǎn)為int型,輸出試試看:

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((int)Color.Yellow);
  9:             Console.ReadLine();
 10:         }
 11:     }
 12:
 13:     enum Color
 14:     {
 15:         Yellow,
 16:         Blue,
 17:         Brown,
 18:         Green
 19:     }
 20: }

結(jié)果輸出:

0

 

從上面的例子中,我們可以看到枚舉的使用,如同static變量一樣,可被直接使用。如不用轉(zhuǎn)換則默認(rèn)輸出枚舉定義的字符,強(qiáng)轉(zhuǎn)后

則輸出枚舉對(duì)應(yīng)的數(shù)字值---故枚舉可表達(dá)恒量數(shù)值,或者命名的字符串標(biāo)示。

 

基礎(chǔ)數(shù)據(jù)類型

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((byte)Color.Yellow);
  9:             Console.WriteLine((byte)Color.Blue);
 10:             Console.ReadLine();
 11:         }
 12:     }
 13:
 14:     enum Color:byte
 15:     {
 16:         Yellow,
 17:         Blue,
 18:         Brown,
 19:         Green
 20:     }
 21: }

 

結(jié)果輸出為:

0

1

這里唯一做的修改是枚舉Color繼承自byte ,而不是默認(rèn)的int型。

枚舉可繼承自數(shù)值型類型,如longulongshortushortintuintbyte sbyte。但是無(wú)法繼承自char類型。

 

枚舉可被枚舉繼承嗎?

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((byte)Color.Yellow);
  9:             Console.WriteLine((byte)Color.Blue);
 10:             Console.ReadLine();
 11:         }
 12:     }
 13:
 14:     enum Color:byte
 15:     {
 16:         Yellow,
 17:         Blue,
 18:         Brown,
 19:         Green
 20:
 21:     }
 22:
 23:     enum Shades:Color
 24:     {
 25:
 26:     }
 27: }

編譯,報(bào)錯(cuò):

Type bytesbyteshortushortintuintlong, or ulong expected.

枚舉可被class繼承嗎?

  1:   enum Color:byte
  2:     {
  3:         Yellow,
  4:         Blue,
  5:         Brown,
  6:         Green
  7:     }
  8:
  9: class Derived:Color
 10:     {
 11:
 12:     }

 

編譯報(bào)錯(cuò):

'Enums.Derived': cannot derive from sealed type 'Enums.Color'

 

接下來(lái),我們看看枚舉和這3個(gè)接口的關(guān)系:IComparableIFormattable 和IConvertible。

A. IComparable

  1: using System;
  2:
  3: namespace Enums
  4: {
  5:     internal enum Color
  6:     {
  7:         Yellow,
  8:         Blue,
  9:         Green
 10:     }
 11:
 12:     internal class Program
 13:     {
 14:         private static void Main(string[] args)
 15:         {
 16:             Console.WriteLine(Color.Yellow.CompareTo(Color.Blue));
 17:             Console.WriteLine(Color.Blue.CompareTo(Color.Green));
 18:             Console.WriteLine(Color.Blue.CompareTo(Color.Yellow));
 19:             Console.WriteLine(Color.Green.CompareTo(Color.Green));
 20:             Console.ReadLine();
 21:         }
 22:     }
 23: }

結(jié)果輸出:

-1

-1

1

0

-1表示小于關(guān)系,0表示等于關(guān)系,1表示大于關(guān)系。這里標(biāo)明了enum默認(rèn)繼承了IComparable接口,故有CompareTo()函數(shù)。

B. IFormattable

  1: using System;
  2:
  3: namespace Enums
  4: {
  5:     internal enum Color
  6:     {
  7:         Yellow,
  8:         Blue,
  9:         Green
 10:     }
 11:
 12:     internal class Program
 13:     {
 14:         private static void Main(string[] args)
 15:         {
 16:             System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "X"));
 17:             System.Console.WriteLine(Color.Format(typeof(Color), Color.Green, "d"));
 18:             Console.ReadLine();
 19:         }
 20:     }
 21: }

結(jié)果輸出:

00000002

2

Format方法繼承自IFormatter 接口,它是一個(gè)static函數(shù),因此可以被枚舉Color直接使用。format需要傳入3個(gè)參數(shù),第一個(gè)是枚舉的類型,

第二個(gè)參數(shù)是枚舉值,第三個(gè)是格式化標(biāo)示---二進(jìn)制、十進(jìn)制等。

C. IConvertible

  1: Hide   Copy Code
  2: using System;
  3:
  4: namespace Enums
  5: {
  6:      enum Color
  7:     {
  8:         Yellow,
  9:         Blue,
 10:         Green
 11:     }
 12:
 13:     internal class Program
 14:     {
 15:         private static void Main(string[] args)
 16:         {
 17:             string[] names;
 18:             names = Color.GetNames(typeof (Color));
 19:             foreach (var name in names)
 20:             {
 21:                 Console.WriteLine(name);
 22:             }
 23:             Console.ReadLine();
 24:         }
 25:     }
 26: }
 27:

結(jié)果輸出:

Yellow

Blue

Green

GetNames函數(shù)是枚舉Color的靜態(tài)方法,用于獲得枚舉所有的字符標(biāo)示名稱集合。

同理也可使用ToString輸出枚舉的字符標(biāo)示:

  1: using System;
  2:
  3: namespace Enums
  4: {
  5:      enum Color
  6:     {
  7:         Yellow,
  8:         Blue,
  9:         Green
 10:     }
 11:
 12:     internal class Program
 13:     {
 14:         private static void Main(string[] args)
 15:         {
 16:            Console.WriteLine(Color.Blue.ToString());
 17:            Console.WriteLine(Color.Green.ToString());
 18:            Console.ReadLine();
 19:         }
 20:     }
 21: }

 

顯示輸出:

Blue

Green

上面的例子顯示,枚舉可在int和string直接轉(zhuǎn)換,這個(gè)特性是枚舉使用中非常重要的一個(gè)功能。

 

試試看,枚舉的字符標(biāo)示是否可以重復(fù)定義:

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((byte)Color.Yellow);
  9:             Console.WriteLine((byte)Color.Blue);
 10:             Console.ReadLine();
 11:         }
 12:     }
 13:
 14:     enum Color
 15:     {
 16:         Yellow,
 17:         Blue,
 18:         Brown,
 19:         Green,
 20:         Blue
 21:     }
 22: }

編譯報(bào)錯(cuò),結(jié)果:

Compile time error: The type 'Enums.Color' already contains a definition for 'Blue'

可見(jiàn)枚舉中不能定義重復(fù)的字符標(biāo)示。

 

再看另外一個(gè)例子:

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((int)Color.Yellow);
  9:             Console.WriteLine((int)Color.Blue);
 10:             Console.WriteLine((int)Color.Brown);
 11:             Console.WriteLine((int)Color.Green);
 12:
 13:             Console.ReadLine();
 14:         }
 15:     }
 16:
 17:     enum Color
 18:     {
 19:         Yellow =2,
 20:         Blue,
 21:         Brown=9,
 22:         Green,
 23:
 24:     }
 25: }

結(jié)果:

2

3

9

10

 

 

從結(jié)果看,我們可以在枚舉定義的時(shí)候重新指定數(shù)值,如我們指定了yellow為2,則Blue默認(rèn)為Yellow+1,為3. 下來(lái),我們指定了Brown為9,則

其下的Green為Brown + 1,為10。 這是一個(gè)有趣的enum特性。

 

如指定的數(shù)據(jù)類型超過(guò)枚舉的定義類型,如何?

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:
  9:         }
 10:     }
 11:
 12:     enum Color:byte
 13:     {
 14:         Yellow =300 ,
 15:         Blue,
 16:         Brown=9,
 17:         Green,
 18:     }
 19: }

編譯報(bào)錯(cuò):

Compile time error: Constant value '300' cannot be converted to a 'byte'

300超出了byte數(shù)據(jù)類型的范圍,故報(bào)錯(cuò)。 枚舉的類型檢測(cè)非常好,在項(xiàng)目使用中很實(shí)用的功能。

 

枚舉引用代碼

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Console.WriteLine((int)Color.Yellow);
  9:             Console.WriteLine((int)Color.Blue);
 10:             Console.WriteLine((int)Color.Brown);
 11:             Console.WriteLine((int)Color.Green);
 12:
 13:             Console.ReadLine();
 14:         }
 15:     }
 16:
 17:     enum Color
 18:     {
 19:         Yellow = 2,
 20:         Blue,
 21:         Brown = 9,
 22:         Green = Yellow
 23:     }
 24: }

結(jié)果輸出:

2

3

9

2

這里,我們定義Green的值,引用了Color的Yellow枚舉值。

枚舉,是否可以在外面修改枚舉值:

  1: using System;
  2: namespace Enums
  3: {
  4:     class Program
  5:     {
  6:         static void Main(string[] args)
  7:         {
  8:             Color.Yellow = 3;
  9:         }
 10:     }
 11:
 12:     enum Color
 13:     {
 14:         Yellow = 2,
 15:         Blue,
 16:         Brown = 9,
 17:         Green = Yellow
 18:     }
 19: }

運(yùn)行結(jié)果:

Compile time error: The left-hand side of an assignment must be a variable, property or indexer

編譯報(bào)錯(cuò)了??梢?jiàn)枚舉數(shù)值是常量,僅在初始化的時(shí)候確定,外部無(wú)法動(dòng)態(tài)修改。

 

那么,枚舉是否可以循環(huán)依賴?

  1: using System;
  2:
  3: namespace Enums
  4: {
  5:     internal enum Color
  6:     {
  7:         Yellow=Blue,
  8:         Blue
  9:     }
 10:
 11:     internal class Program
 12:     {
 13:         private static void Main(string[] args)
 14:         {
 15:         }
 16:     }
 17: }

編譯結(jié)果:

Compile time error: The evaluation of the constant value for 'Enums.Color.Yellow' involves a circular definition

保留關(guān)鍵字

深入淺出OOP(六): 理解C#的Enums

  1: using System;
  2:
  3: namespace Enums
  4: {
  5:      enum Color
  6:     {
  7:       value__
  8:     }
  9:
 10:     internal class Program
 11:     {
 12:         private static void Main(string[] args)
 13:         {
 14:
 15:         }
 16:     }
 17: }

編譯報(bào)錯(cuò):

Compile time error: The enumerator name 'value__' is reserved and cannot be used

原因很簡(jiǎn)單,這里的value__是保留關(guān)鍵字。

枚舉小結(jié):

  1. enum表達(dá)了恒定的數(shù)值,枚舉類型可以用字符串標(biāo)示

  2. 無(wú)法聲明char基礎(chǔ)類型的枚舉

  3. enum僅僅能繼承自bytesbyteshortushortintuintlong, 或ulong數(shù)據(jù)類型

  4. 默認(rèn)的,enum是一個(gè)sealed類,既無(wú)法被繼承

  5. enum類型隱式實(shí)現(xiàn)了System.Enum

  6. enum類型繼承了3個(gè)接口:IComparableIFormattable和IConvertible

  7. enum中,數(shù)字和字符串可以互相轉(zhuǎn)換

  8. enum的值可被初始化為同樣的值

  9. enum的值要在初始化時(shí)候確定

  10. enum中,'value__'關(guān)鍵字不能使用

 

原文:Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)

 

文章目錄:

  • 深入淺出OOP(一): 多態(tài)和繼承(早期綁定/編譯時(shí)多態(tài))

  • 深入淺出OOP(二): 多態(tài)和繼承(繼承)

  • 深入淺出OOP(三): 多態(tài)和繼承(動(dòng)態(tài)綁定/運(yùn)行時(shí)多態(tài))

  • 深入淺出OOP(四): 多態(tài)和繼承(抽象類)

  • 深入淺出OOP(五): C#訪問(wèn)修飾符(Public/Private/Protected/Internal/Sealed/Constants)

  • 深入淺出OOP(六): 理解C#的Enums


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI