




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1 / 701. 傳入某個屬性的set方法的隱含參數的名稱是什么?value,它的類型和屬性所聲名的類型相同。 2. 如何在C#中實現繼承?在類名后加上一個冒號,再加上基類的名稱。 3. C#支持多重繼承么?不支持。可以用接口來實現。 4. 被protected修飾的屬性/方法在何處可以訪問?在繼承或間接繼承與這個類的子類中可以訪問。 5. 私有成員會被繼承么?會,但是不能被訪問。所以看上去他們似乎是不能被繼承的,但實際上確實被繼承了。 6. 請描述一下修飾符protected internal。 被protected interna
2、l修飾的屬性/方法只能在它的在同一個程序集(Assembly)中的子類被訪問。 7. C#提供一個默認的無參數構造函數,當我實現了另外一個有一個參數的構造函數時候,還想保留這個無參數的構造函數。這樣我應該寫幾個構造函數?兩個,一旦你實現了一個構造函數,C#就不會再提供默認的構造函數了,所以需要手動實現那個無參數構造函數。 8. C#中所有對象共同的基類是什么?System.Object. 9. 重載和覆寫有什么區別?重載提供了對一個方法簽名的不同參數調用的實現。覆寫提供了子類中改變父類方法行為的實現。 10. 在方法定義中,virtual有什么含意? 被
3、virtual修飾的方法可以被子類覆寫。 11. 能夠將非靜態的方法覆寫成靜態方法么?不能,覆寫方法的簽名必須與被覆寫方法的簽名保持一致,除了將virtual改為override。 12. 可以覆寫私有的虛方法么?不可以,甚至子類中無法訪問父類中的私有方法。 13. 能夠阻止某一個類被其他類繼承么? 可以,使用關鍵字sealed。 14. 能夠實現允許某個類被繼承,但不允許其中的某個方法被覆寫么? 可以,標記這個類為public,并標記這個方法為sealed。 15. 什么是抽象類(abstract class)?一種不可以被實例化的類。抽象類
4、中一般含有抽象方法,當然也可有具體實現。繼承類只有實現過所有抽象類的抽象方法后才能被實例化。 16. 何時必須聲明一個類為抽象類? 當這個類中包含抽象方法時,或是該類并沒有完全實現父類的抽象方法時。 17. 接口(interface)是什么?只含有共有抽象方法(public abstract method)的類。這些方法必須在子類中被實現。 18. 為什么不能指定接口中方法的修飾符? 接口中的方法用來定義對象之間通信的契約,指定接口中的方法為私有或保護沒有意義。他們默認為公有方法。 19. 可以繼承多個接口么? 當然。 20. 那么如果這些接口
5、中有重復的方法名稱呢? 這種情況中你可以決定如何實現。當然需要特別得小心。但是在編譯環節是沒有問題的。 21. 接口和抽象類的區別是什么? 接口中所有方法必須是抽象的,并且不能指定方法的訪問修飾符。抽象類中可以有方法的實現,也可以指定方法的訪問修飾符。 22. 如何區別重載方法? 不同的參數類型,不同的參數個數,不同的參數順序。 23. const和readonly有什么區別?const關鍵字用來聲明編譯時常量,readonly用來聲明運行時常量。 24. System.String 和System.StringBuilder有什么區別?System.S
6、tring是不可變的字符串。System.StringBuilder存放了一個可變的字符串,并提供一些對這個字符串修改的方法。 1.靜態成員和非靜態成員的區別?2.const 和 static readonly 區別?3.extern 是什么意思?4.abstract 是什么意思?5.internal 修飾符起什么作用?6.sealed 修飾符是干什么的?7.override 和 overload 的區別?8.什么是索引指示器?9.new 修飾符是起什么作用?10.this 關鍵字的含義?11.可以使用抽象函數重寫基類中的虛函數嗎?12.密封類可以有虛函數嗎?13.什么是屬性訪問器?14.ab
7、stract 可以和 virtual 一起使用嗎?可以和 override 一起使用嗎?15.接口可以包含哪些成員?16.類和結構的區別?17.接口的多繼承會帶來哪些問題?18.抽象類和接口的區別?19.別名指示符是什么?20.如何手工釋放資源?21.P/Invoke是什么?22.StringBuilder 和 String 的區別?23.explicit 和 implicit 的含義?24.params 有什么用?25.什么是反射? 以下是我做的一份參考答案(C# 語言范疇之內),如果有不準確、不全面的,歡迎各位朋友指正! 1.靜態成員和非靜態成員的區別?答:靜態變量使用 stat
8、ic 修飾符進行聲明,在類被實例化時創建,通過類進行訪問不帶有 static 修飾符聲明的變量稱做非靜態變量,在對象被實例化時創建,通過對象進行訪問一個類的所有實例的同一靜態變量都是同一個值,同一個類的不同實例的同一非靜態變量可以是不同的值靜態函數的實現里不能使用非靜態成員,如非靜態變量、非靜態函數等示例:using System;using System.Collections.Generic;using System.Text; namespace Example01 class Program
9、 class Class1 public static String staticStr = "Class" public String notstaticS
10、tr = "Obj" static void Main(string args) /靜態變量通過類進行訪問,該類所有實例的同一靜態變量都是同一個值
11、; Console.WriteLine("Class1's staticStr: 0", Class1.staticStr); Class1 tmpObj1 = new Class1(); tmpObj1.not
12、staticStr = "tmpObj1" Class1 tmpObj2 = new Class1(); tmpObj2.notstaticStr = "tmpObj2"
13、60; /非靜態變量通過對象進行訪問,不同對象的同一非靜態變量可以有不同的值 Console.WriteLine("tmpObj1's notstaticStr: 0", tmpObj1.notstaticStr); Console.WriteLine("tmpObj2's n
14、otstaticStr: 0", tmpObj2.notstaticStr); Console.ReadLine(); 結果:Class1's staticStr: ClasstmpObj1's notstaticStr: tmpObj1tmpObj2's notstaticStr: tmpObj2
15、2.const 和 static readonly 區別?答:const用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序static readonly用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運行期初始化示例:測試類:using System;using System.Collections.Generic;using System.Text; namespace Example02Lib public c
16、lass Class1 public const String strConst = "Const" public static readonly String strStaticReadonly = "StaticReadonly" /public const Str
17、ing strConst = "Const Changed" /public static readonly String strStaticReadonly = "StaticReadonly Changed" 客戶端代碼:using System;using System.Collections.Generic;using System.Text;using Example02Lib; namespace Ex
18、ample02 class Program static void Main(string args) /修改Example02中Class1的strConst初始值后,只編譯Example02Lib項目
19、0; /然后到資源管理器里把新編譯的Example02Lib.dll拷貝Example02.exe所在的目錄,執行Example02.exe /切不可在IDE里直接調試運行因為這會重新編譯整個解決方案! /可以看到s
20、trConst的輸出沒有改變,而strStaticReadonly的輸出已經改變 /表明Const變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運行時初始化的 Console.WriteLine("strConst : 0", Class1.strConst); &
21、#160; Console.WriteLine("strStaticReadonly : 0", Class1.strStaticReadonly); Console.ReadLine(); 結果:strConst :
22、 ConststrStaticReadonly : StaticReadonly 修改后的示例:測試類:using System;using System.Collections.Generic;using System.Text; namespace Example02Lib public class Class1 /public const String strConst = "Const" &
23、#160; /public static readonly String strStaticReadonly = "StaticReadonly" public const String strConst = "Const Changed" public static readonly String strStaticReadon
24、ly = "StaticReadonly Changed" 結果strConst : ConststrStaticReadonly : StaticReadonly Changed3.extern 是什么意思?答:extern 修飾符用于聲明由程序集外部實現的成員函數經常用于系統API函數的調用(通過 DllImport )。注意,和DllImport一起使用時要加上 static 修飾符也可以用于對于同一程序集不同版本組件的調用(用 extern 聲明別名)不能與 abstract 修飾符同時使用示例:using System;usin
25、g System.Collections.Generic;using System.Text;using System.Runtime.InteropServices; namespace Example03 class Program /注意DllImport是一個Attribute Property,在System.Runtime.InteropServices命名空間中定義 &
26、#160; /extern與DllImport一起使用時必須再加上一個static修飾符 DllImport("User32.dll") public static extern int MessageBox(int Handle, string Message, string Caption, int Type);
27、; static int Main() string myString; Console.Write("Enter your message: ");
28、0; myString = Console.ReadLine(); return MessageBox(0, myString, "My Message Box", 0); 結果: 4.abstract 是什么意思?答:abstract 修飾符
29、可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員abstract 不可以和 static 、virtual 一起使用聲明為 abstract 成員可以不包括實現代碼,但只要類中還有未實現的抽象成員(即抽象類),那么它的對象就不能被實例化,通常用于強制繼承類必須實現某一成員示例:using System;using System.Collections.Generic;using System.Text; namespace Example04 #region 基類,抽象類 public
30、 abstract class BaseClass /抽象屬性,同時具有get和set訪問器表示繼承類必須將該屬性實現為可讀寫 public abstract String Attribute
31、0; get; set; /抽象方法,傳入一個字符串參數無返回值 public abstract void Function(String value);
32、 /抽象事件,類型為系統預定義的代理(delegate):EventHandler public abstract event EventHandler Event; /抽象索引指示器,只具有get訪問器表示繼承類必須將該索引指示器實現為只讀 public abstract
33、Char thisint Index get; #endregion #region 繼承類 public class DeriveCla
34、ss : BaseClass private String attribute; public override String Attribute get
35、; return attribute;
36、60; set attribute = value; &
37、#160; public override void Function(String value) attribute = value; if (Event != null)
38、160; Event(this, new EventArgs();
39、 public override event EventHandler Event; public override Char thisint Index get
40、160; return attributeIndex; #endregion
41、 class Program static void OnFunction(object sender, EventArgs e) for (int i = 0; i < (DeriveClass)send
42、er).Attribute.Length; i+) Console.WriteLine(DeriveClass)sender)i);
43、0; static void Main(string args) DeriveClass tmpObj = new DeriveClass();
44、0; tmpObj.Attribute = "1234567" Console.WriteLine(tmpObj.Attribute); /將靜態函數OnFunction與tmpObj對象的Event事件進行關聯
45、0; tmpObj.Event += new EventHandler(OnFunction); tmpObj.Function("7654321"); Console.ReadLine();
46、160; 結果:12345677654321 5.internal 修飾符起什么作用?答:internal 修飾符可以用于類型或成員,使用該修飾符聲明的類型或成員只能在同一程集內訪問接口的成員不能使用 internal 修飾符值得注意的是,如果為 internal 成員加上了 protected 修飾符,這時的訪問級別為 internal 或 protected。只是看字面意思容易弄錯,許多人認為 internal protected 應該是“只有同一個程序集中的子類可以訪問”,但其實它表示“同一
47、個程序集中的所有類,以及所有程序集中的子類都可以訪問”示例Example05Lib 項目的 Class1 using System;using System.Collections.Generic;using System.Text; namespace Example05Lib public class Class1 internal String strInternal = null;
48、160; public String strPublic; internal protected String strInternalProtected = null; 結果Example05Lib 項目的 Class2 類可以訪問到 Class1 的 strInternal 成員,當然也可以訪問到 strInternalProtected 成員,因為他們在同一個程序集里 Example05 項目里的 Class3 類無法訪問到
49、Class1 的 strInternal 成員,因為它們不在同一個程序集里。但卻可以訪問到 strInternalProtected 成員,因為 Class3 是 Class1 的繼承類 Example05 項目的 Program 類既無法訪問到 Class1 的 strInternal 成員,也無法訪問到 strInternalProtected 成員,因為它們既不在同一個程序集里也不存在繼承關系 6.sealed 修飾符是干什么的?答:sealed 修飾符表示密封用于類時,表示該類不能再被繼承,不能和 abstract 同時使用,因為這兩個修飾符在含義上互相排斥用于方法
50、和屬性時,表示該方法或屬性不能再被重寫,必須和 override 關鍵字一起使用,因為使用 sealed 修飾符的方法或屬性肯定是基類中相應的虛成員通常用于實現第三方類庫時不想被客戶端繼承,或用于沒有必要再繼承的類以防止濫用繼承造成層次結構體系混亂恰當的利用 sealed 修飾符也可以提高一定的運行效率,因為不用考慮繼承類會重寫該成員示例:using System;using System.Collections.Generic;using System.Text; namespace Example06 class Program
51、160; class A public virtual void F()
52、60; Console.WriteLine("A.F"); public virtual void G()
53、160; Console.WriteLine("A.G"); class B :
54、A public sealed override void F()
55、Console.WriteLine("B.F"); public override void G()
56、0; Console.WriteLine("B.G"); class C : B &
57、#160; public override void G() Console.WriteLine("C.G");
58、; static void Main(string args) new A().F(); &
59、#160; new A().G(); new B().F(); new B().G(); new C().F();
60、60; new C().G(); Console.ReadLine(); 結果:類 B 在繼承類 A 時可以重寫兩個虛函數,如圖所示: 由于類 B 中對 F 方法進行了密封, 類 C 在繼承類 B 時只能
61、重寫一個函數,如圖所示: 控制臺輸出結果,類 C 的方法 F 只能是輸出 類B 中對該方法的實現:A.FA.GB.FB.GB.FC.G 7.override 和 overload 的區別?答:override 表示重寫,用于繼承類對基類中虛成員的實現overload 表示重載,用于同一個類中同名方法不同參數(包括類型不同或個數不同)的實現示例:using System;using System.Collections.Generic;using System.Text; namespace Example07 class Program&
62、#160; class BaseClass public virtual void F()
63、 Console.WriteLine("BaseClass.F"); class DeriveClass : BaseClass
64、160; public override void F() base.F(); &
65、#160; Console.WriteLine("DeriveClass.F"); public void Add(int Left, int Right)&
66、#160; Console.WriteLine("Add for Int: 0", Left + Right);
67、0; public void Add(double Left, double Right) Console.WriteLine("Add for int: 0", Lef
68、t + Right); static void Main(string args)
69、0; DeriveClass tmpObj = new DeriveClass(); tmpObj.F(); tmpObj.Add(1, 2); tmpObj.Add(1.1, 2.2);
70、160; Console.ReadLine(); 結果:BaseClass.FDeriveClass.FAdd for Int: 3Add for int: 3.3 8.什么是索引指示器?答:實現索引指示器(indexer)的類可以象數組那樣使用其實例后的對象,但與數組不同的是索引指示器的參數類型不僅限于int簡單來說,其本質就是一個含參數屬性示例: us
71、ing System;using System.Collections.Generic;using System.Text; namespace Example08 public class Point private double x, y; public Point(double X, double Y) &
72、#160; x = X; y = Y; /重寫ToString方法方便輸出
73、60; public override string ToString() return String.Format("X: 0 , Y: 1", x, y); public c
74、lass Points Point points; public Points(Point Points) points = Points;
75、 public int PointNumber get
76、; return points.Length; /實現索引訪問器
77、 public Point thisint Index get
78、60; return pointsIndex; /感謝watson hua( /索引指示器的實質是含參屬性,參數并不只限于int class WeatherOfWeek
79、160; public string thisint Index get
80、0; /注意case段使用return直接返回所以不需要break switch (Index)
81、; case 0:
82、160; return "Today is cloudy!" &
83、#160; case 5:
84、0; return "Today is thundershower!"
85、160; default:
86、0; return "Today is fine!"
87、; public string thisstring Day
88、160; get string TodayWeather = null;
89、; /switch的標準寫法 switch (Day)
90、160; case "Sunday":
91、 TodayWeather = "Today is cloudy!" break;
92、60; case "Friday": &
93、#160; TodayWeather = "Today is thundershow
94、er!" break;
95、160; default:
96、60; TodayWeather = "Today is fine!"
97、60; break;
98、 return TodayWeather; class Program
99、60; static void Main(string args) Point tmpPoints = new Point10; for (int i = 0; i < tmpPoints.Length; i+)
100、0; tmpPointsi = new Point(i, Math.Sin(i);
101、; Points tmpObj = new Points(tmpPoints); for (int i = 0; i < tmpObj.PointNumber; i+)
102、160; Console.WriteLine(tmpObji); string Week = new string "Sunday", "Monday", "Tuesday",
103、"Wednesday", "Thursday", "Friday", "Staurday" WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek(); for (int i = 0; i < 6; i+
104、) Console.WriteLine(tmpWeatherOfWeeki); &
105、#160; foreach (string tmpDay in Week) Console.WriteLine(tmpWeatherOfWeektmpDay);
106、60; Console.ReadLine(); 結果:X: 0 , Y: 0X: 1 , Y: 0.841470984807897X: 2 , Y: 0.909297426825682X: 3 , Y: 0.141120008059867X: 4 , Y: -0.7568024953
107、07928X: 5 , Y: -0.958924274663138X: 6 , Y: -0.279415498198926X: 7 , Y: 0.656986598718789X: 8 , Y: 0.989358246623382X: 9 , Y: 0.412118485241757Today is cloudy!Today is fine!Today is fine!Today is fine!Today is fine!Today is thundershower!Today is cloudy!Today is fine!Today is fine!Today is fine!Today
108、 is fine!Today is thundershower!Today is fine! 9.new 修飾符是起什么作用?答:new 修飾符與 new 操作符是兩個概念new 修飾符用于聲明類或類的成員,表示隱藏了基類中同名的成員。而new 操作符用于實例化一個類型new 修飾符只能用于繼承類,一般用于彌補基類設計的不足new 修飾符和 override 修飾符不可同時用在一個成員上,因為這兩個修飾符在含義上互相排斥示例:using System;using System.Collections.Generic;using System.Text; namespace
109、Example09 class BaseClass /基類設計者聲明了一個PI的公共變量,方便進行運算 public static double PI = 3.1415; class DervieClass : BaseClass
110、160; /繼承類發現該變量的值不能滿足運算精度,于是可以通過new修飾符顯式隱藏基類中的聲明 public new static double PI = 3.1415926; class Program static void Main(string arg
111、s) Console.WriteLine(BaseClass.PI); Console.WriteLine(DervieClass.PI); &
112、#160; Console.ReadLine(); 結果:3.14153.1415926 10.this 關鍵字的含義?答:this 是一個保留字,僅限于構造函數和方法成員中使用在類的構造函數中出現表示對正在構造的對象本身的引用,在類的方法中出現表示對調用該方法的對象的引用,在結構的構造上函數中出現表示對正在構造的結構的引用,在結構的方法中出現表示對調用該方法的結果的引用this 保留字不能用于靜態成員的實現里,因為這時對象或結構并未實例化在 C#
113、系統中,this 實際上是一個常量,所以不能使用 this+ 這樣的運算this 保留字一般用于限定同名的隱藏成員、將對象本身做為參數、聲明索引訪問器、判斷傳入參數的對象是否為本身示例:using System;using System.Collections.Generic;using System.Text; namespace Example10 class Class1 private double c;
114、160; private string value; public double C get
115、 return c; public Class1(double c)
116、 /限定同名的隱藏成員 this.c = c; public Class1(Class1 value) &
117、#160; /用對象本身實例化自己沒有意義 if (this != value) c = value.C;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫療設備公司發熱病人處置流程
- 2025年度旅游行業安全保障總結與計劃
- 2025-2030中國甲基多巴行業市場發展趨勢與前景展望戰略研究報告
- 教育行業在線教育質量提升與評估方案
- 六年級信息技術能力提升計劃
- 針對五年級的語文復習計劃建議
- 2025年大學輔導員考試題庫:學生綜合素質評價體系在輔導員工作中的實踐與應用試題
- 2025年聲樂演唱職業能力測試卷:聲樂作品演唱與音樂教育改革試題
- 2025-2030中國桂圓酒市場運行態勢與競爭格局分析研究報告版
- 汽車電控懸架系統技術解析
- 艾灸培訓初級班
- 220kV變電站電氣設備常規交接試驗方案
- 算法設計與分析 課件 7.10-回溯法 - 典型應用 - 兩種實現 - n皇后問題
- 九年級道德與法治上冊 第二單元 民主與法治 第四課 建設法治中國教案 新人教版
- 防性侵安全教育課件
- 《食品儀器分析技術》項目七質譜法及其在食品分析中的應用
- 北京市2024年中考歷史真題試卷(含答案)
- 職業技能大賽-鴻蒙移動應用開發賽初賽理論知識考試及答案
- 2024年全國高考日語試卷(新題型)(含答案與解析)
- 部編版六年級下冊《第14課 文言文二則》2024年同步練習卷
- 報銷單據明細表Excel模板
評論
0/150
提交評論