大家好,我是SUNWEN,現在是五月四號23:15,再過十五分鐘就要熄燈了.所以要抓緊時間,先開個頭,明天繼續. 現在我要說的是C#中的用戶自定義轉換(User-Defined Conversions),其中用到了前面說的struct的知識,就是結構呀,忘了嗎?好,沒忘就好.從我們以下的課程我們可以看到結構的用處(剛才我還在想它有什么用,呵呵).用class聲明的是一個類,而用struct聲明的可以看作是一個類型,對,就是像C#自帶的int,short,long那樣的類型了. C#中可以允許我們對結構(struct)和類(class)進行轉換,所以我們可以在其中定義一些轉換.但是,C#規定,所有的轉換聲明都必須在顯示(explicit)和隱示(implicit)中選擇一個.比方說,我們用這個語句的時候 int a=10; System.Console.println(a): 就用到了int的隱示的轉換toString.如果是(String)a,就叫做顯示.所以,顯/隱之差就在于是否表現出來.大家現在肯定還是一頭霧水,等到明天我把例子寫出來再分析一下就清楚了,要熄燈了,我先走一步了! 喔~~~~~終于起來了,五月五日8:45.下面給出例子,在這個例子中,一個名為RomanNumeral的類型被聲明,然后對他實施了好幾種轉換.
000: // UserConversions\conversion.cs 001: using System; 002: 003: struct RomanNumeral 004: { 005: public RomanNumeral(int value) 006: { 007: this.value = value; 008: } 009: static public implicit operator RomanNumeral(int value) 010: { 011: return new RomanNumeral(value); 012: } 013: static public explicit operator int(RomanNumeral roman) 014: { 015: return roman.value; 016: } 017: static public implicit operator string(RomanNumeral roman) 018: { 019: return("Conversion not yet implemented"); 020: } 021: private int value; 022: } 023: 024: class Test 025: { 026: static public void Main() 027: { 028: RomanNumeral numeral; 029: 030: numeral = 10; 031: 032: // 顯式地從numeral到int的轉換033: Console.WriteLine((int)numeral); 034: 035: // 隱示地轉換到string036: Console.WriteLine(numeral); 037: 038: // 顯示地轉換到int,然后顯示地轉換到short040: short s = (short)numeral; 041: 042: Console.WriteLine(s); 043: 044: } 045: } 這個例子子的輸出是: 10 Conversion not yet implemented 10 注意009和013的operator操作符,它是一個轉換操作符.static public explicit operator int(RomanNumeral roman),記住這樣的形式,它就代表了一個轉換.再看第033行,因為在前面int這個轉換被聲明成了explicit,即顯示地,所以,在使用這個轉換時,必須用括號. 下面再給出一個例子,這個例子聲明了兩個結構,RomanNumeral和BinaryNumeral,然后在它們之間進行轉換. 000: // UserConversions\structconversion.cs 001: using System; 002: 003: struct RomanNumeral 004: { 005: public RomanNumeral(int value) { this.value = value; } 006: static public implicit operator RomanNumeral(int value) 007: {return new RomanNumeral(value);} 008: static public implicit operator 009: RomanNumeral(BinaryNumeral binary) 010: {return new RomanNumeral((int)binary);} 011: static public explicit operator int(RomanNumeral roman) 012: {return roman.value;} 013: static public implicit operator string(RomanNumeral roman) 014: {return("Conversion not yet implemented");} 015: private int value; 016: } 017: 018: struct BinaryNumeral 019: { 020: public BinaryNumeral(int value) {this.value = value;} 021: 022: static public implicit operator BinaryNumeral(int value) 023: {return new BinaryNumeral(value);} 024: static public implicit operator string(BinaryNumeral binary) 025: {return("Conversion not yet implemented");} 026: static public explicit operator int(BinaryNumeral binary) 027: {return(binary.value);} 028: 029: private int value; 030: } 031: 032: class Test 033: { 034: static public void Main() 035: { 036: RomanNumeral roman; 037: roman = 10; 038: BinaryNumeral binary; 039: binary = (BinaryNumeral)(int)roman; 040: roman = binary; 041: Console.WriteLine((int)binary); 042: Console.WriteLine(binary); 043: } 044: } 這個例子的輸出是: 10 Conversion not yet implemented 注意,第039行并沒有直接由RomanNumeral轉化成BinaryNumeral,因為沒有直接的轉換提供.所以先把RomanNumeral轉換成int,再轉成BinaryNumeral.其余的東西跟上面的例子是一樣的(至少我這么認為),如果上面的例子理解了,下面的就好了. OK,又完了一節,學了這么多,大家有什么感覺呢,歡迎和我交流,mrfat@china.com
|