版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、類的方法(函數(shù)),函數(shù)的更多內(nèi)容(2),結構函數(shù),回憶一下第五章的結構類型,我們可以使用它在一個變量中存儲多個不同的數(shù)據(jù)域,并通過.的形式加以訪問,如:,Date today; today.Year = 2006; today.Month = Months.Sep; today.Day = 25; today.Week = Days.Mon;,結構函數(shù),當我們需要按格式輸出today變量中的內(nèi)容時,我們可以在Main函數(shù)中寫這樣一段代碼:,static void Main(string args) Date today; today.Year = 2006; today.Month = Mon
2、ths.Sep; today.Day = 25; today.Week = Days.Mon; Console.WriteLine(“0 1, 2”, today.Month, today.Day, today.Year); ,Sep 25, 2006,結構函數(shù),但當我們需要輸出兩個Date型變量中的內(nèi)容時,我們需要寫兩遍輸出格式化輸出代碼:,static void Main(string args) Date today, tomorrow; today.Year = 2006; today.Month = Months.Sep; today.Day = 25; today.Week = D
3、ays.Mon; tomorrow = today; tomorrow.Day+; tomorrow.Week+; Console.WriteLine(“0 1, 2”, today.Month, today.Day, today.Year); Console.WriteLine(“0 1, 2”, tomorrow.Month, tomorrow.Day, tomorrow.Year); ,結構函數(shù),在這里,我們可以把格式化日期數(shù)據(jù)的代碼放在一個單獨的函數(shù)中進行處理,如:,static string ToFormatDate(Date theDate) return theDate.Mont
4、h + “ “ + theDate.Day + “, “ + theDate.Year; static void Main(string args) Date today, tomorrow; / Initialsize today and tomorrow variable. Console.WriteLine(ToFormatDate(today); Console.WriteLine(ToFormatDate(tomorrow); ,結構函數(shù),或者還有一種更為優(yōu)雅的方法 使用結構函數(shù) 結構體中除可以存放數(shù)據(jù)外,還可以把函數(shù)寫在結構體中,稱其為結構函數(shù)或成員函數(shù)。 通過把函數(shù)放入結構體中,
5、可以方便的處理結構體中的數(shù)據(jù),使函數(shù)的語意更為清晰、明了。 結構函數(shù)的定義與使用與普通函數(shù)類似,沒有什么特別之處,唯一不同的是結構函數(shù)能夠自由讀寫結構類型中的數(shù)據(jù)成員。,結構函數(shù),結構函數(shù)的定義方法:,struct public ; public ; / . public ( ) / 在這里可以直接訪問結構成員變量; public ( ) / 在這里可以直接訪問結構成員變量; ,結構函數(shù),使用結構函數(shù)實現(xiàn)日期數(shù)據(jù)的格式化功能:,struct Date public ushort Year; public Months Month; public byte Day; public string
6、ToFormatDate() return Month + “ “ + Day + “, “ + Year; static void Main(string args) Date today; today.Year = 2006; today.Month = Months.Sep; today.Day = 25; Console.WriteLine(today.ToFormatDate(); ,結構函數(shù),增加函數(shù):,struct Date public int Year,Month,Day; public Date AddDay() Date tomorrow; tomorrow.Year =
7、 Year; tomorrow.Month = Month; tomorrow.Day = Day+1; return tomorrow; static void Main(string args) Date today, tomorrow; today.Year = 2006; today.Month = 9; today.Day = 25; tomorrow = today.AddDay(); Console.WriteLine(“Tomorrow is: 0”, today.ToFormatDate(); ,類中的方法(函數(shù)),換成類中的方法(函數(shù)):,class Date public
8、 int Year,Month,Day; public Date AddDay() Date tomorrow = new Date(); tomorrow.Year = Year; tomorrow.Month = Month; tomorrow.Day = Day+1; return tomorrow; static void Main(string args) Date today = new Date(); today.Year = 2006; today.Month = 9; today.Day = 25; Date tomorrow = today.AddDay(); Consol
9、e.WriteLine(today.ToFormatDate(); Console.WriteLine(tomorrow.ToFormatDate(); ,結構函數(shù),在上述結構的基礎上完成如下作業(yè): 實現(xiàn)Date結構體,其中包含年、月、日三個數(shù)據(jù)。 實現(xiàn)增加一天、增加一月、增加一年的功能。 實現(xiàn)減少一天、減少一月、減少一年的功能。 處理日期溢出問題,如天數(shù)大于31,月數(shù)大于12,或天數(shù)小于1,月數(shù)小于1等問題。 實現(xiàn)兩日期相加功能。 實現(xiàn)日期格式化功能。,結構函數(shù),使用示例:,static void Main(string args) Date today, tomorrow, yesterd
10、ay; today.Year = 2006; today.Month = Months.Sep; today.Day = 25; tomorrow = today;tomorrow.AddDay(); yesterday = today;yesterday.SubtractDay(); Console.WriteLine(“Yesterday is: 0”, yesterday.ToFormatDate(); Console.WriteLine(“Today is: 0”, today.ToFormatDate(); Console.WriteLine(“Tomorrow is: 0”, to
11、morrow.ToFormatDate(); ,函數(shù)的重載,求和函數(shù)的實現(xiàn):,static int Sum(int numbers) int sum; foreach (int element in numbers) sum += element; return sum; static void Main(string args) int numbers = 1, 2, 3, 4, 5, 6, 7, 8, 9 ; Console.WriteLine(Sum(numbers); ,函數(shù)的重載,該函數(shù)存在著缺陷,只能使用它對整數(shù)進行求和,如要進行浮點數(shù)求和,就需實現(xiàn)求和函數(shù)的浮點版版:,stati
12、c double DoubleArraySum(double numbers) double sum; foreach (double element in numbers) sum += element; return sum; ,函數(shù)的重載,這樣,在使用求和函數(shù)進行求和時,就需要記住求和函數(shù)的不同版本:,static void Main(string args) int intNumbers = 1, 3, 5, 7, 9 ; double doubleNumbers = 3.14159; 1.414, 2.71828 ; Console.WriteLine(IntArraySum(int
13、Numbers); Console.WriteLine(DoubleArraySum(doubleNumbers); ,整數(shù)數(shù)組的求和,浮點數(shù)數(shù)組的求和,函數(shù)的重載,觀察控制臺輸出函數(shù),我們可以使用它輸出多種類型的數(shù)據(jù):,static void Main(string args) Console.Write(1); Console.Write(true); Console.Write(3.14159); Console.Write(“Hello world!”); Console.Write(“Hello, 0”, “somebody”); ,整數(shù),邏輯值,浮點數(shù),字符串,不確定數(shù)目參數(shù),函數(shù)
14、的重載,通過對以上示例的觀察,我們發(fā)現(xiàn),可以使用不同類型、不同數(shù)量的參數(shù)來調(diào)用Write函數(shù)。 這樣,我們只需使用Write函數(shù)就能輸出輸出多種類型的數(shù)據(jù),而不需要記住不同類型的輸出函數(shù)的名字。 以上這些特性是通過函數(shù)的重載來實現(xiàn)的,這樣可以大大的方便我對函數(shù)的使用,不再需要記憶大量不同的函數(shù)名字了。,函數(shù)的重載,什么是函數(shù)的重載 函數(shù)的重載是指多個函數(shù)使用同一個名字對函數(shù)進行命名。 重載的函數(shù)是通過參數(shù)的類型和數(shù)量來加以區(qū)分的。轉(zhuǎn)而去調(diào)用特定的函數(shù)。 重載函數(shù)的參數(shù)類型、參數(shù)數(shù)量、返回值類型都可以不相同。 為了對重載函數(shù)進行區(qū)分,重載的函數(shù)必需在參數(shù)類型或參數(shù)數(shù)量上有所不同,而單單只是返回類
15、型不同是無法對重載函數(shù)進行區(qū)分的。,函數(shù)的重載,函數(shù)重載示例,static int Sum(int numbers) int sum; foreach (int element in numbers) sum += element; return sum; static double Sum(double numbers) double sum; foreach (double element in numbers) sum += element; return sum; ,參數(shù)和返回 值的類型不同,處理整數(shù)累加,處理浮點數(shù)累加,函數(shù)的重載,函數(shù)重載示例 在這里分別使用Sum函數(shù)的int和do
16、uble版進行求和,但可使用相同的名字進行調(diào)用。,static void Main(string args) int intNumbers = 1, 3, 5, 7, 9 ; double doubleNumbers = 3.14159, 1.414, 2.71828 ; int intNumbersSum = Sum(intNumbers); double doubleNumbersSum = Sum(doubleNumbers); Console.WriteLine(“Int numbers sum: 0”, intNumbersSum); Console.WriteLine(“Doubl
17、e numbers sum: 1”, doubleNumbersSum); ,函數(shù)的重載,函數(shù)重載示例,static void WriteLine(int intVal) / Code in here. static void WriteLine(bool boolVal) / Code in here. static void WriteLine(double doubleVal) / Code in here. static void WriteLine(string formatString,) / Code in here. static void WriteLine(string f
18、ormatString, params string outArgs) / Code in here. ,構造函數(shù)的重載,構造函數(shù)重載示例,public class Animal private string name; public Animal() name = 動物; public Animal(string name) = name; ,委 托,什么是委托 委托與 C 語言中的函數(shù)指針非常類似,可以使用委托去調(diào)用函數(shù),并可方便的修改委托所引用的函數(shù)。 可以把委托理解成函數(shù)的別名,也就是說使用另一個名字來調(diào)用同一個函數(shù),就像是調(diào)用該函數(shù)本身一樣,本質(zhì)上沒什么區(qū)別,只是使用不同的名字而已。 定義委托時,要聲明委托所能引用的函數(shù)的類型,即委托所能引用的函數(shù)的參數(shù)類型和返回類型等。,委 托,委托的定義方法 委托定義示例 委托使用示例 委托也是一種類型,因此,委托應定義在命名空間中,與 class 平級。,delegate ( );,delegate void ProcessDelegate(double var1, double val2);,ProcessDeleg
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026廣西南寧市婦女兒童活動中心招聘筆試備考試題及答案解析
- 2025川北醫(yī)學院選調(diào)工作人員3人(公共基礎知識)綜合能力測試題附答案
- 2025廣東茂名市電白區(qū)招聘大學生鄉(xiāng)村醫(yī)生1人備考題庫附答案
- 2026西北工業(yè)大學動力與能源學院爆震燃燒團隊非事業(yè)編人員招聘1人(陜西)筆試備考試題及答案解析
- 2025年河北秦皇島市公開選聘事業(yè)單位工作人員1名(公共基礎知識)測試題附答案
- 2026浙江金華市武義縣醫(yī)療保障局招聘4人筆試備考題庫及答案解析
- 2026云南玉溪市華寧縣公共就業(yè)和人才服務中心招聘公益性崗位人員3人筆試模擬試題及答案解析
- 2025秋人教版道德與法治八年級上冊第一單元單元單元思考與行動課件
- 2026上海復旦大學馬克思主義學院招聘2人筆試參考題庫及答案解析
- 2026年甘肅省隴南市康縣周家壩鎮(zhèn)專業(yè)化管理村文書招聘筆試參考題庫及答案解析
- 廣西2025年高等職業(yè)教育考試全區(qū)模擬測試 能源動力與材料 大類試題及逐題答案解說
- 2026江蘇省公務員考試公安機關公務員(人民警察)歷年真題匯編附答案解析
- 2026年失眠患者睡眠調(diào)理指南
- 2026年盤錦職業(yè)技術學院單招職業(yè)適應性測試題庫及答案詳解一套
- 2025年10月自考00610高級日語(二)試題及答案
- 2026年包頭鐵道職業(yè)技術學院單招職業(yè)技能考試題庫帶答案解析
- 循證護理在基礎護理中的應用
- 復旦大學招生面試常見問題及回答要點
- 危險化學品兼容性矩陣表
- 道路交通法律課件
- 老年人營養(yǎng)不良篩查與營養(yǎng)支持方案
評論
0/150
提交評論