sunwen教程之----c#進階
(九)
[email protected]
大家好,我是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,又完了一節,學了這么多,大家有什么感覺呢,歡迎和我交流,[email protected]
下一頁