




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、6.sealed 修飾符是干什么的?答:sealed 修飾符表示密封用于類時,表示該類不能再被繼承,不能和 abstract 同時使用,因為這兩個修飾符在含義上互相排斥用于方法和屬性時,表示該方法或?qū)傩圆荒茉俦焕^承,必須和 override 關(guān)鍵字一起使用,因為使用 sealed 修飾符的方法或?qū)傩钥隙ㄊ腔愔邢鄳?yīng)的虛成員通常用于實現(xiàn)第三方類庫時不想被客戶端繼承,或用于沒有必要再繼承的類以防止濫用繼承造成層次結(jié)構(gòu)體系混亂恰當(dāng)?shù)睦?sealed 修飾符也可以提高一定的運行效率,因為不用考慮繼承類會重寫該成員示例: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ù),如圖所示:由于類 B 中對 F 方法進行了密封, 類 C 在繼承類 B
4、時只能重寫一個函數(shù),如圖所示:控制臺輸出結(jié)果,類 C 的方法 F 只能是輸出 類B 中對該方法的實現(xiàn):A.FA.GB.FB.GB.FC.G 7.override 和 overload 的區(qū)別?答:override 表示重寫,用于繼承類對基類中虛成員的實現(xiàn)overload 表示重載,用于同一個類中同名方法不同參數(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.什么是索引指示器?答:實現(xiàn)索引指
7、示器(indexer)的類可以象數(shù)組那樣使用其實例后的對象,但與數(shù)組不同的是索引指示器的參數(shù)類型不僅限于int簡單來說,其本質(zhì)就是一個含參數(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; /實現(xiàn)索引訪問器 public Point thisint Index get
14、60; return pointsIndex; /感謝watson hua( /索引指示器的實質(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 操作符是兩個
43、概念new 修飾符用于聲明類或類的成員,表示隱藏了基類中同名的成員。而new 操作符用于實例化一個類型new 修飾符只能用于繼承類,一般用于彌補基類設(shè)計的不足new 修飾符和 override 修飾符不可同時用在一個成員上,因為這兩個修飾符在含義上互相排斥示例:using System;using System.Collections.Generic;using System.Text; namespace Example09 class BaseClass /基類設(shè)計者聲明了一個PI的公共變量,方便進行運算 public static double PI = 3.1415; cla
44、ss DervieClass : BaseClass /繼承類發(fā)現(xiàn)該變量的值不能滿足運算精度,于是可以通過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òu)造函數(shù)和方法成員中使用在類的構(gòu)造函數(shù)中出現(xiàn)表示對正在構(gòu)造的對象本身的引用,在類的方法中出現(xiàn)表示對調(diào)用該方法的對象的引用,在結(jié)構(gòu)的構(gòu)造上函數(shù)中出現(xiàn)表示對正在構(gòu)造的結(jié)構(gòu)的引用,
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 分布式能源系統(tǒng)的可持續(xù)發(fā)展研究-洞察闡釋
- 上海海事職業(yè)技術(shù)學(xué)院《中外政治經(jīng)濟制度比較》2023-2024學(xué)年第二學(xué)期期末試卷
- 湖南都市職業(yè)學(xué)院《中國文學(xué)名著選講》2023-2024學(xué)年第二學(xué)期期末試卷
- 石家莊工商職業(yè)學(xué)院《節(jié)奏訓(xùn)練III》2023-2024學(xué)年第二學(xué)期期末試卷
- 江西應(yīng)用技術(shù)職業(yè)學(xué)院《無機及分析化》2023-2024學(xué)年第二學(xué)期期末試卷
- 防災(zāi)科技學(xué)院《內(nèi)科學(xué)F》2023-2024學(xué)年第二學(xué)期期末試卷
- 廣州美術(shù)學(xué)院《房屋建筑學(xué)與城市規(guī)劃》2023-2024學(xué)年第二學(xué)期期末試卷
- 玉溪師范學(xué)院《專項理論與實踐III》2023-2024學(xué)年第二學(xué)期期末試卷
- 四川護理職業(yè)學(xué)院《建筑環(huán)境與能源系統(tǒng)測試技術(shù)》2023-2024學(xué)年第二學(xué)期期末試卷
- 肇慶醫(yī)學(xué)高等??茖W(xué)?!夺t(yī)學(xué)科研設(shè)計與訓(xùn)練》2023-2024學(xué)年第二學(xué)期期末試卷
- GB/T 7778-2017制冷劑編號方法和安全性分類
- GB/T 40393-2021金屬和合金的腐蝕奧氏體不銹鋼晶間腐蝕敏感性加速腐蝕試驗方法
- GB/T 32210-2015便攜式氣相色譜-質(zhì)譜聯(lián)用儀技術(shù)要求及試驗方法
- GB/T 31765-2015高密度纖維板
- GB/T 18682-2002物理氣相沉積TiN薄膜技術(shù)條件
- 直播實訓(xùn)室設(shè)備清單表模板
- 實詞辨析與成語辨析
- 項目一乙烯生產(chǎn)過程課件
- 三位數(shù)乘一位數(shù)練習(xí)題(300道)
- 高職英語課程說課稿課件
- 政府投資項目審計與報告案例信息講解課件
評論
0/150
提交評論