六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

SUNWEN圖文說明教程之----C#進(jìn)階(9)

[摘要]大家好,我是SUNWEN,現(xiàn)在是五月四號(hào)23:15,再過十五分鐘就要熄燈了.所以要抓緊時(shí)間,先開個(gè)頭,明天繼續(xù).現(xiàn)在我要說的是C#中的用戶自定義轉(zhuǎn)換(User-Defined Conversions),其中用到了前面說的struct的知識(shí),就是結(jié)構(gòu)呀,忘了嗎?好,沒忘就好.從我們以下的課程我們可以...

大家好,我是SUNWEN,現(xiàn)在是五月四號(hào)23:15,再過十五分鐘就要熄燈了.所以要抓緊時(shí)間,先開個(gè)頭,明天繼續(xù).

現(xiàn)在我要說的是C#中的用戶自定義轉(zhuǎn)換(User-Defined Conversions),其中用到了前面說的struct的知識(shí),就是結(jié)構(gòu)呀,忘了嗎?好,沒忘就好.從我們以下的課程我們可以看到結(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è).比方說,我們用這個(gè)語句的時(shí)候
int a=10;
System.Console.println(a):
就用到了int的隱示的轉(zhuǎn)換toString.如果是(String)a,就叫做顯示.所以,顯/隱之差就在于是否表現(xiàn)出來.大家現(xiàn)在肯定還是一頭霧水,等到明天我把例子寫出來再分析一下就清楚了,要熄燈了,我先走一步了!


喔~~~~~終于起來了,五月五日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行并沒有直接由RomanNumeral轉(zhuǎn)化成BinaryNumeral,因?yàn)闆]有直接的轉(zhuǎn)換提供.所以先把RomanNumeral轉(zhuǎn)換成int,再轉(zhuǎn)成BinaryNumeral.其余的東西跟上面的例子是一樣的(至少我這么認(rèn)為),如果上面的例子理解了,下面的就好了.

OK,又完了一節(jié),學(xué)了這么多,大家有什么感覺呢,歡迎和我交流,[email protected]

 




主站蜘蛛池模板: 午夜精品久久久久久中宇 | 做a小视频| 亚洲欧美日韩在线2020 | 亚洲成人自拍网 | 天天色天天草 | 四虎国产精品永久在线网址 | 野外三级国产在线观看 | 青青草国产精品久久 | 午夜视频在线免费观看 | 性欧美大战久久久久久久 | 日韩大片免费观看 | 五月花精品视频在线观看 | 中文字幕亚洲综久久2021 | 四虎影院com | 亚洲九色 | 天天摸夜夜添狠狠添2018 | 亚洲h在线观看 | 日韩国产欧美成人一区二区影院 | 手机看片日韩在线 | 色综合久久中文字幕综合网 | 啪啪网站免费看 | 中文字幕乱码一二三四区 | 午夜在线观看免费影院 | 亚洲综合激情丁香六月 | 欧美综合图片一区二区三区 | 亚洲女人网 | 伊人久久大 | 欧美性理论片在线观看片免费 | 日韩精品亚洲专区在线观看 | 天天摸夜夜添久久精品麻豆 | 啪啪午夜 | 欧洲黄色大片 | 日本高清免费一本视频无需下载 | 婷婷激情综合网 | 亚洲天堂热 | 日韩精品无码一区二区三区 | 日本高清在线播放 | 一区二区三区在线播放视频 | 日本天堂网在线观看 | 日日夜夜摸| 日韩欧美亚洲每日更新网 |