




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、6.sealed 修飾符是干什么的?答:sealed 修飾符表示密封用于類時(shí),表示該類不能再被繼承,不能和 abstract 同時(shí)使用,因?yàn)檫@兩個(gè)修飾符在含義上互相排斥用于方法和屬性時(shí),表示該方法或?qū)傩圆荒茉俦焕^承,必須和 override 關(guān)鍵字一起使用,因?yàn)槭褂?sealed 修飾符的方法或?qū)傩钥隙ㄊ腔愔邢鄳?yīng)的虛成員通常用于實(shí)現(xiàn)第三方類庫(kù)時(shí)不想被客戶端繼承,或用于沒有必要再繼承的類以防止濫用繼承造成層次結(jié)構(gòu)體系混亂恰當(dāng)?shù)睦?sealed 修飾符也可以提高一定的運(yùn)行效率,因?yàn)椴挥每紤]繼承類會(huì)重寫該成員示例:using System;using System.Collections.Gen
2、eric;using System.Text; namespace Example06 class Program class A public virtual void F() Console.WriteLine("A.F"); public virtual void G() Console.WriteLine("A.G"); class B : A public sealed override void F() Console.WriteLine("B.F"); public override void G() Cons
3、ole.WriteLine("B.G"); class C : B public override void G() Console.WriteLine("C.G"); static void Main(string args) new A().F(); new A().G(); new B().F(); new B().G(); new C().F(); new C().G(); Console.ReadLine(); 結(jié)果:類 B 在繼承類 A 時(shí)可以重寫兩個(gè)虛函數(shù),如圖所示:由于類 B 中對(duì) F 方法進(jìn)行了密封, 類 C 在繼承類 B
4、時(shí)只能重寫一個(gè)函數(shù),如圖所示:控制臺(tái)輸出結(jié)果,類 C 的方法 F 只能是輸出 類B 中對(duì)該方法的實(shí)現(xiàn):A.FA.GB.FB.GB.FC.G 7.override 和 overload 的區(qū)別?答:override 表示重寫,用于繼承類對(duì)基類中虛成員的實(shí)現(xiàn)overload 表示重載,用于同一個(gè)類中同名方法不同參數(shù)(包括類型不同或個(gè)數(shù)不同)的實(shí)現(xiàn)示例:using System;using System.Collections.Generic;using System.Text; namespace Example07 class Program class BaseClass publi
5、c virtual void F() Console.WriteLine("BaseClass.F"); class DeriveClass : BaseClass public override void F() base.F(); Console.WriteLine("DeriveClass.F"); public void Add(int Left, int Right) Console.WriteLine("Add for Int: 0", Left + Right); public void Add(double Left,
6、 double Right) Console.WriteLine("Add for int: 0", Left + Right); static void Main(string args) DeriveClass tmpObj = new DeriveClass(); tmpObj.F(); tmpObj.Add(1, 2); tmpObj.Add(1.1, 2.2); Console.ReadLine(); 結(jié)果:BaseClass.FDeriveClass.FAdd for Int: 3Add for int: 3.3 8.什么是索引指示器?答:實(shí)現(xiàn)索引指
7、示器(indexer)的類可以象數(shù)組那樣使用其實(shí)例后的對(duì)象,但與數(shù)組不同的是索引指示器的參數(shù)類型不僅限于int簡(jiǎn)單來(lái)說(shuō),其本質(zhì)就是一個(gè)含參數(shù)屬性示例: using System;using System.Collections.Generic;using System.Text; namespace Example08 public class Point private double x, y;
8、0; public Point(double X, double Y) x = X; y = Y;
9、60; /重寫ToString方法方便輸出 public override string ToString() return String.Format("X: 0 , Y: 1", x, y);
10、; public class Points Point points; public Points(Point Points)
11、; points = Points; public int PointNumber get
12、 return points.Length;
13、60; /實(shí)現(xiàn)索引訪問器 public Point thisint Index get
14、60; return pointsIndex; /感謝watson hua( /索引指示器的實(shí)質(zhì)是含參
15、屬性,參數(shù)并不只限于int class WeatherOfWeek public string thisint Index get
16、; /注意case段使用return直接返回所以不需要break switch (Index)
17、 case 0:
18、160; return "Today is cloudy!"
19、160; case 5:
20、0; return "Today is thundershower!"
21、60; default:
22、; return "Today is fine!"
23、 public string thiss
24、tring Day get string TodayWeather
25、= null; /switch的標(biāo)準(zhǔn)寫法 switch (Day)
26、60; case "Sunday":
27、 TodayWeather = "Today is cloudy!"
28、0; break;
29、160; case "Friday":
30、 TodayWeather = "Today is thundershower!" break;
31、160; default:
32、0; TodayWeather = "Today is fine!"
33、60; break; &
34、#160; return TodayWeather;
35、 class Program static void Main(string args) Point tmpPoints = new Point10; &
36、#160; for (int i = 0; i < tmpPoints.Length; i+) tmpPointsi = new Point(i, Math.Sin(i);
37、 Points tmpObj = new Points(tmpPoints); for (int i = 0; i < tmpObj.PointNumber; i+)
38、60; Console.WriteLine(tmpObji); string W
39、eek = new string "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday" WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek();
40、; for (int i = 0; i < 6; i+) Console.WriteLine(tmpWeatherOfWeeki);
41、160; foreach (string tmpDay in Week) Con
42、sole.WriteLine(tmpWeatherOfWeektmpDay); Console.ReadLine(); 結(jié)果:9.new 修飾符是起什么作用?答:new 修飾符與 new 操作符是兩個(gè)
43、概念new 修飾符用于聲明類或類的成員,表示隱藏了基類中同名的成員。而new 操作符用于實(shí)例化一個(gè)類型new 修飾符只能用于繼承類,一般用于彌補(bǔ)基類設(shè)計(jì)的不足new 修飾符和 override 修飾符不可同時(shí)用在一個(gè)成員上,因?yàn)檫@兩個(gè)修飾符在含義上互相排斥示例:using System;using System.Collections.Generic;using System.Text; namespace Example09 class BaseClass /基類設(shè)計(jì)者聲明了一個(gè)PI的公共變量,方便進(jìn)行運(yùn)算 public static double PI = 3.1415; cla
44、ss DervieClass : BaseClass /繼承類發(fā)現(xiàn)該變量的值不能滿足運(yùn)算精度,于是可以通過(guò)new修飾符顯式隱藏基類中的聲明 public new static double PI = 3.1415926; class Program static void Main(string args) Console.WriteLine(BaseClass.PI); Console.WriteLine(DervieClass.PI); Console.ReadLine(); 結(jié)果:3.14153.1415926 10.this 關(guān)鍵字的含義?答:this 是一個(gè)保留字,僅限于構(gòu)造函數(shù)和方法成員中使用在類的構(gòu)造函數(shù)中出現(xiàn)表示對(duì)正在構(gòu)造的對(duì)象本身的引用,在類的方法中出現(xiàn)表示對(duì)調(diào)用該方法的對(duì)象的引用,在結(jié)構(gòu)的構(gòu)造上函數(shù)中出現(xiàn)表示對(duì)正在構(gòu)造的結(jié)構(gòu)的引用,
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 養(yǎng)殖原料服務(wù)合同樣本
- 電拖課后答案學(xué)習(xí)資料
- 個(gè)人抵押個(gè)人合同標(biāo)準(zhǔn)文本
- 債務(wù)車輛頂賬合同標(biāo)準(zhǔn)文本
- 買賣房屋定金合同樣本
- 個(gè)人買賣收購(gòu)合同樣本
- 內(nèi)部房屋借用合同樣本
- 冷庫(kù)使用合同樣本樣本
- 冰箱購(gòu)貨合同樣本
- 內(nèi)裝拆除合同標(biāo)準(zhǔn)文本
- 宗教事務(wù)條例
- GB/T 498-2014石油產(chǎn)品及潤(rùn)滑劑分類方法和類別的確定
- 人物志學(xué)習(xí)撒迦利亞201509
- GB/T 31765-2015高密度纖維板
- 學(xué)生宿舍帶班領(lǐng)導(dǎo)及值班教師巡查登記表
- GB/T 15103-2008林用絞盤機(jī)
- 議論要有針對(duì)性 課件
- 四年級(jí)古詩(shī)詞大賽課件
- 醫(yī)療機(jī)構(gòu)及傳染病衛(wèi)生監(jiān)督知識(shí)培訓(xùn)課件
- 玫瑰糠疹課件
- 奧本海姆《信號(hào)與系統(tǒng)(第二版)》習(xí)題參考答案
評(píng)論
0/150
提交評(píng)論