直播中
現(xiàn)在我要說(shuō)的是C#中的用戶自定義轉(zhuǎn)換(User-Defined Conversions),其中用到了前面說(shuō)的struct的知識(shí),就是結(jié)構(gòu)呀,忘了嗎?好,沒(méi)忘就好.從我們以下的課程我們可以看到結(jié)構(gòu)的用處(剛才我還在想它有什么用,呵呵).用class聲明的是一個(gè)類,而用struct聲明的可以看作是一個(gè)類型,對(duì),就是像C#自帶的int,short,long那樣的類型了.
C#中可以允許我們對(duì)結(jié)構(gòu)(struct)和類(class)進(jìn)行轉(zhuǎn)換,所以我們可以在其中定義一些轉(zhuǎn)換.但是,C#規(guī)定,所有的轉(zhuǎn)換聲明都必須在顯示(explicit)和隱示(implicit)中選擇一個(gè).比方說(shuō),我們用這個(gè)語(yǔ)句的時(shí)候
int a=10;
System.Console.println(a):
就用到了int的隱示的轉(zhuǎn)換toString.如果是(String)a,就叫做顯示.所以,顯/隱之差就在于是否表現(xiàn)出來(lái).大家現(xiàn)在肯定還是一頭霧水,等到明天我把例子寫出來(lái)再分析一下就清楚了,要熄燈了,我先走一步了!
喔~~~~~終于起來(lái)了,五月五日8:45.下面給出例子,在這個(gè)例子中,一個(gè)名為RomanNumeral的類型被聲明,然后對(duì)他實(shí)施了好幾種轉(zhuǎn)換.
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的轉(zhuǎn)換033: Console.WriteLine((int)numeral);
034:
035: // 隱示地轉(zhuǎn)換到string036: Console.WriteLine(numeral);
037:
038: // 顯示地轉(zhuǎn)換到int,然后顯示地轉(zhuǎn)換到short040: short s = (short)numeral;
041:
042: Console.WriteLine(s);
043:
044: }
045: }
這個(gè)例子子的輸出是:
10
Conversion not yet implemented
10
注意009和013的operator操作符,它是一個(gè)轉(zhuǎn)換操作符.static public explicit operator int(RomanNumeral roman),記住這樣的形式,它就代表了一個(gè)轉(zhuǎn)換.再看第033行,因?yàn)樵谇懊鎖nt這個(gè)轉(zhuǎn)換被聲明成了explicit,即顯示地,所以,在使用這個(gè)轉(zhuǎn)換時(shí),必須用括號(hào).
下面再給出一個(gè)例子,這個(gè)例子聲明了兩個(gè)結(jié)構(gòu),RomanNumeral和BinaryNumeral,然后在它們之間進(jìn)行轉(zhuǎn)換.
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: }
這個(gè)例子的輸出是:
10
Conversion not yet implemented
注意,第039行并沒(méi)有直接由RomanNumeral轉(zhuǎn)化成BinaryNumeral,因?yàn)闆](méi)有直接的轉(zhuǎn)換提供.所以先把RomanNumeral轉(zhuǎn)換成int,再轉(zhuǎn)成BinaryNumeral.其余的東西跟上面的例子是一樣的(至少我這么認(rèn)為),如果上面的例子理解了,下面的就好了.
OK,又完了一節(jié),學(xué)了這么多,大家有什么感覺呢,歡迎和我交流,mrfat@china.com