C程序員面試必答.doc_第1頁
C程序員面試必答.doc_第2頁
C程序員面試必答.doc_第3頁
C程序員面試必答.doc_第4頁
C程序員面試必答.doc_第5頁
已閱讀5頁,還剩23頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

C# 程序員面試必答1.靜態(tài)變量和非靜態(tài)變量的區(qū)別? 答: 靜態(tài)變量: 靜態(tài)變量使用 static 修飾符進(jìn)行聲明 在所屬類被裝載時(shí)創(chuàng)建 通過類進(jìn)行訪問 所屬類的所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值 非靜態(tài)變量: 不帶有 static 修飾符聲明的變量稱做非靜態(tài)變量 在類被實(shí)例化時(shí)創(chuàng)建 通過對(duì)象進(jìn)行訪問 同一個(gè)類的不同實(shí)例的同一非靜態(tài)變量可以是不同的值 示例: using System;using System.Collections.Generic;using System.Text;namespace Example01 class Program class Class1 public static String staticStr = "Class" public String notstaticStr = "Obj" static void Main(string args) /靜態(tài)變量通過類進(jìn)行訪問,該類所有實(shí)例的同一靜態(tài)變量都是同一個(gè)值 Console.WriteLine("Class1s staticStr: 0", Class1.staticStr); Class1 tmpObj1 = new Class1(); tmpObj1.notstaticStr = "tmpObj1" Class1 tmpObj2 = new Class1(); tmpObj2.notstaticStr = "tmpObj2" /非靜態(tài)變量通過對(duì)象進(jìn)行訪問,不同對(duì)象的同一非靜態(tài)變量可以有不同的值 Console.WriteLine("tmpObj1s notstaticStr: 0", tmpObj1.notstaticStr); Console.WriteLine("tmpObj2s notstaticStr: 0", tmpObj2.notstaticStr); Console.ReadLine(); 復(fù)制代碼結(jié)果: Class1s staticStr: Class tmpObj1s notstaticStr: tmpObj1 tmpObj2s notstaticStr: tmpObj2 2.const 和 static readonly 區(qū)別? 答: const 用 const 修飾符聲明的成員叫常量,是在編譯期初始化并嵌入到客戶端程序 static readonly 用 static readonly 修飾符聲明的成員依然是變量,只不過具有和常量類似的使用方法:通過類進(jìn)行訪問、初始化后不可以修改。但與常量不同的是這種變量是在運(yùn)行期初始化 示例: 測(cè)試類: using System;using System.Collections.Generic;using System.Text;namespace Example02Lib public class Class1 public const String strConst = "Const" public static readonly String strStaticReadonly = "StaticReadonly" /public const String strConst = "Const Changed" /public static readonly String strStaticReadonly = "StaticReadonly Changed" /5-1-a-s-p-x復(fù)制代碼客戶端代碼: using System;using System.Collections.Generic;using System.Text;using Example02Lib;namespace Example02 class Program static void Main(string args) /修改Example02中Class1的strConst初始值后,只編譯Example02Lib項(xiàng)目 /然后到資源管理器里把新編譯的Example02Lib.dll拷貝Example02.exe所在的目錄,執(zhí)行Example02.exe /切不可在IDE里直接調(diào)試運(yùn)行因?yàn)檫@會(huì)重新編譯整個(gè)解決方案! /可以看到strConst的輸出沒有改變,而strStaticReadonly的輸出已經(jīng)改變 /表明Const變量是在編譯期初始化并嵌入到客戶端程序,而StaticReadonly是在運(yùn)行時(shí)初始化的 Console.WriteLine("strConst : 0", Class1.strConst); Console.WriteLine("strStaticReadonly : 0", Class1.strStaticReadonly); Console.ReadLine(); 復(fù)制代碼結(jié)果: strConst : Const strStaticReadonly : StaticReadonly 修改后的示例: 測(cè)試類: using System;using System.Collections.Generic;using System.Text;namespace Example02Lib public class Class1 /public const String strConst = "Const" /public static readonly String strStaticReadonly = "StaticReadonly" public const String strConst = "Const Changed" public static readonly String strStaticReadonly = "StaticReadonly Changed" 復(fù)制代碼結(jié)果 strConst : Const strStaticReadonly : StaticReadonly Changed 3.extern 是什么意思? 答: extern 修飾符用于聲明由程序集外部實(shí)現(xiàn)的成員函數(shù) 經(jīng)常用于系統(tǒng)API函數(shù)的調(diào)用(通過 DllImport )。注意,和DllImport一起使用時(shí)要加上 static 修飾符 也可以用于對(duì)于同一程序集不同版本組件的調(diào)用(用 extern 聲明別名) 不能與 abstract 修飾符同時(shí)使用51aspx 示例: using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace Example03 class Program /注意DllImport是一個(gè)Attribute Property,在System.Runtime.InteropServices命名空間中定義 /extern與DllImport一起使用時(shí)必須再加上一個(gè)static修飾符 DllImport("User32.dll") public static extern int MessageBox(int Handle, string Message, string Caption, int Type); static int Main() string myString; Console.Write("Enter your message: "); myString = Console.ReadLine(); return MessageBox(0, myString, "My Message Box", 0); 復(fù)制代碼結(jié)果: 4.abstract 是什么意思? 答: abstract 修飾符可以用于類、方法、屬性、事件和索引指示器(indexer),表示其為抽象成員 abstract 不可以和 static 、virtual 、override 一起使用 聲明為 abstract 成員可以不包括實(shí)現(xiàn)代碼,但只有類中還有未實(shí)現(xiàn)的抽象成員,該類就不可以被實(shí)例化,通常用于強(qiáng)制繼承類必須實(shí)現(xiàn)某一成員 示例: using System;using System.Collections.Generic;using System.Text;namespace Example04 #region 基類,抽象類 public abstract class BaseClass /抽象屬性,同時(shí)具有g(shù)et和set訪問器表示繼承類必須將該屬性實(shí)現(xiàn)為可讀寫 public abstract String Attribute get; set; /抽象方法,傳入一個(gè)字符串參數(shù)無返回值 public abstract void Function(String value); /抽象事件,類型為系統(tǒng)預(yù)定義的代理(delegate):EventHandler public abstract event EventHandler Event; /抽象索引指示器,只具有g(shù)et訪問器表示繼承類必須將該索引指示器實(shí)現(xiàn)為只讀 public abstract Char thisint Index get; #endregion #region 繼承類 public class DeriveClass : BaseClass private String attribute; public override String Attribute get return attribute; set attribute = value; public override void Function(String value) attribute = value; if (Event != null) Event(this, new EventArgs(); public override event EventHandler Event; public override Char thisint Index get return attributeIndex; #endregion class Program static void OnFunction(object sender, EventArgs e) for (int i = 0; i (DeriveClass)sender).Attribute.Length; i+) Console.WriteLine(DeriveClass)sender)i); static void Main(string args) DeriveClass tmpObj = new DeriveClass(); tmpObj.Attribute = "1234567" Console.WriteLine(tmpObj.Attribute); /將靜態(tài)函數(shù)OnFunction與tmpObj對(duì)象的Event事件進(jìn)行關(guān)聯(lián) tmpObj.Event += new EventHandler(OnFunction); tmpObj.Function("7654321"); Console.ReadLine(); 復(fù)制代碼結(jié)果: 1234567 7 6 5 4 3 2 1 5.internal 修飾符起什么作用? 答: internal 修飾符可以用于類型或成員,使用該修飾符聲明的類型或成員只能在同一程集內(nèi)訪問 接口的成員不能使用 internal 修飾符 示例 Example05Lib 項(xiàng)目的 Class1 using System;using System.Collections.Generic;using System.Text;namespace Example05Lib public class Class1 internal String strInternal = null; public String strPublic; 復(fù)制代碼結(jié)果 Example05Lib 項(xiàng)目的 Class2 類可以訪問到 Class1 的 strInternal 成員 Example05 項(xiàng)目的 Program 類無法訪問到 Class1 的 strInternal 成員 6.sealed 修飾符是干什么的? 答: sealed 修飾符表示密封 用于類時(shí),表示該類不能再被繼承,不能和 abstract 同時(shí)使用,因?yàn)檫@兩個(gè)修飾符在含義上互相排斥 用于方法和屬性時(shí),表示該方法或?qū)傩圆荒茉俦焕^承,必須和 override 關(guān)鍵字一起使用,因?yàn)槭褂?sealed 修飾符的方法或?qū)傩钥隙ㄊ腔愔邢鄳?yīng)的虛成員 通常用于實(shí)現(xiàn)第三方類庫時(shí)不想被客戶端繼承,或用于沒有必要再繼承的類以防止濫用繼承造成層次結(jié)構(gòu)體系混亂 恰當(dāng)?shù)睦?sealed 修飾符也可以提高一定的運(yùn)行效率,因?yàn)椴挥每紤]繼承類會(huì)重寫該成員 示例: using System;using System.Collections.Generic;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() Console.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(); 復(fù)制代碼結(jié)果: 類 B 在繼承類 A 時(shí)可以重寫兩個(gè)虛函數(shù),如圖所示: 由于類 B 中對(duì) F 方法進(jìn)行了密封, 類 C 在繼承類 B 時(shí)只能重寫一個(gè)函數(shù),如圖所示: 控制臺(tái)輸出結(jié)果,類 C 的方法 F 只能是輸出 類B 中對(duì)該方法的實(shí)現(xiàn): A.F A.G B.F B.G B.F C.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 public 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, 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(); 復(fù)制代碼結(jié)果: BaseClass.F DeriveClass.F Add for Int: 3 Add for int: 3.3 8.什么是索引指示器? 答: 實(shí)現(xiàn)索引指示器(indexer)的類可以象數(shù)組那樣使用其實(shí)例后的對(duì)象,但與數(shù)組不同的是索引指示器的參數(shù)類型不僅限于int 簡(jiǎn)單來說,其本質(zhì)就是一個(gè)含參數(shù)屬性 示例: using System;using System.Collections.Generic;using System.Text;namespace Example08 public class Point private double x, y; public Point(double X, double Y) x = X; y = Y; /重寫ToString方法方便輸出 public override string ToString() return String.Format("X: 0 , Y: 1", x, y); public class Points Point points; public Points(Point Points) points = Points; public int PointNumber get return points.Length; /實(shí)現(xiàn)索引訪問器 public Point thisint Index get return pointsIndex; /感謝watson hua(/)的指點(diǎn) /索引指示器的實(shí)質(zhì)是含參屬性,參數(shù)并不只限于int class WeatherOfWeek public string thisint Index get /注意case段使用return直接返回所以不需要break switch (Index) case 0: return "Today is cloudy!" case 5: return "Today is thundershower!" default: return "Today is fine!" public string thisstring Day get string TodayWeather = null; /switch的標(biāo)準(zhǔn)寫法 switch (Day) case "Sunday": TodayWeather = "Today is cloudy!" break; case "Friday": TodayWeather = "Today is thundershower!" break; default: TodayWeather = "Today is fine!" break; return TodayWeather; class Program static void Main(string args) Point tmpPoints = new Point10; for (int i = 0; i tmpPoints.Length; i+) tmpPointsi = new Point(i, Math.Sin(i); Points tmpObj = new Points(tmpPoints); for (int i = 0; i tmpObj.PointNumber; i+) Console.WriteLine(tmpObji); string Week = new string "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Staurday" WeatherOfWeek tmpWeatherOfWeek = new WeatherOfWeek(); for (int i = 0; i 6; i+) Console.WriteLine(tmpWeatherOfWeeki); foreach (string tmpDay in Week) Console.WriteLine(tmpWeatherOfWeektmpDay); Console.ReadLine(); 復(fù)制代碼結(jié)果: X: 0 , Y: 0 X: 1 , Y: 0.841470984807897 X: 2 , Y: 0.909297426825682 X: 3 , Y: 0.141120008059867 X: 4 , Y: -0.756802495307928 X: 5 , Y: -0.958924274663138 X: 6 , Y: -0.279415498198926 X: 7 , Y: 0.656986598718789 X: 8 , Y: 0.989358246623382 X: 9 , Y: 0.412118485241757 Today 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 is fine! Today is thundershower! Today is fine! 9.new 修飾符是起什么作用? 答: new 修飾符與 new 操作符是兩個(gè)概念 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; class DervieClass : BaseClass /繼承類發(fā)現(xiàn)該變量的值不能滿足運(yù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(); 復(fù)制代碼結(jié)果: 3.1415 3.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)的引用,在結(jié)構(gòu)的方法中出現(xiàn)表示對(duì)調(diào)用該方法的結(jié)果的引用 this 保留字不能用于靜態(tài)成員的實(shí)現(xiàn)里,因?yàn)檫@時(shí)對(duì)象或結(jié)構(gòu)并未實(shí)例化 在 C# 系統(tǒng)中,this 實(shí)際上是一個(gè)常量,所以不能使用 this+ 這樣的運(yùn)算 this 保留字一般用于限定同名的隱藏成員、將對(duì)象本身做為參數(shù)、聲明索引訪問器、判斷傳入?yún)?shù)的對(duì)象是否為本身 示例: using System;using System.Collections.Generic;using System.Text;namespace Example10 class Class1 private double c; private string value; public double C get return c; public Class1(double c) /限定同名的隱藏成員 this.c = c; public Class1(Class1 value) /用對(duì)象本身實(shí)例化自己沒有意義 if (this != value) c = value.C; public override string ToString() /將對(duì)象本身做為參數(shù) return string.Format("0 Celsius = 1 Fahrenheit", c, UnitTransClass.C2F(this); /由于好奇,在這做了一個(gè)效率測(cè)試,想看看到底哪種方式訪問成員變量更快,結(jié)論:區(qū)別不大。 public string Test1() long vTickCount = Environment.TickCount; for (int i = 0; i 10000000; i+) this.value = i.ToString(); return string.Format("Have this.: 0 MSEL", Environment.TickCount - vTickCount); public string Test2() long vTickCount = Environment.TickCount; for (int i = 0; i 10000000; i+) value = i.ToString(); return string.Format("Dont have this.: 0 MSEL", Environment.TickCount - vTickCount); class UnitTransClass public static double C2F(Class1 value) /攝氏到華氏的轉(zhuǎn)換公式 return 1.8 * value.C + 32; class Program static void Main(string args) Class1 tmpObj = new Class1(37.5); Console.WriteLine(tmpObj); Console.WriteLine(tmpObj.Test1(); Console.WriteLine(tmpObj.Test2(); Console.ReadLine(); 復(fù)制代碼結(jié)果: 37.5 Celsius = 99.5 Fahrenheit Have this.: 4375 MSEL Dont have this.: 4406 MSEL 11.可以使用抽象函數(shù)重寫基類中的虛函數(shù)嗎? 答: 可以,但需使用 new 修飾符顯式聲明,表示隱藏了基類中該函數(shù)的實(shí)現(xiàn) 示例:class BaseClass public virtual void F() Console.WriteLine("BaseClass.F"); abstract class DeriveClass : BaseClass public new abstract void F(); 復(fù)制代碼12.密封類可以有虛函數(shù)嗎? 答: 可以,基類中的虛函數(shù)將隱式的轉(zhuǎn)化為非虛函數(shù),但密封類本身不能再增加新的虛函數(shù) 示例: class BaseClass public virtual

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論