版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、精選優(yōu)質(zhì)文檔-傾情為你奉上高級C#開發(fā)技術(shù)習(xí)題1.用enum定義字節(jié)類型的方位常量,打印出某一方位并將此方位值轉(zhuǎn)化為字節(jié)類型,字符串類型值。分析輸出結(jié)果的原因。回答以下問題:Enum的缺省類型是什么?回答:Enum在C#中是一種值類型(Value Type),其基類型必須是整數(shù)類型(如Int16)直接輸出myDirection和(byte)myDirection有何區(qū)別?;卮穑哼@是符號名和常數(shù)值的互相轉(zhuǎn)換,當(dāng)枚舉變量轉(zhuǎn)換為常數(shù)值時(shí),必須使用強(qiáng)制轉(zhuǎn)換。class Variables enum orientation : byte north = 1, south = 2, east = 3, w
2、est = 4 static void Main(string args) byte directionByte; string directionString; orientation myDirection = orientation.north; Console.WriteLine("myDirection = 0", myDirection); directionByte = (byte)myDirection; directionString = Convert.ToString(myDirection); Console.WriteLine("byte
3、 equivalent = 0", directionByte); Console.WriteLine("string equivalent = 0", directionString);Console.ReadLine(); 2建立使用關(guān)系運(yùn)算符和邏輯運(yùn)算符的程序文件。Main方法中實(shí)例代碼如下static void Main(string args) Console.WriteLine("Enter an integer:"); int myInt = Convert.ToInt32(Console.ReadLine(); Console.
4、WriteLine("Integer less than 10? 0", myInt < 10); Console.WriteLine("Integer between 0 and 5? 0", (0 <= myInt) && (myInt <= 5); Console.WriteLine("Bitwise AND of Integer and 10 = 0", myInt & 10); Console.ReadLine(); 編譯運(yùn)行該程序。并嘗試myInt輸入不同范圍整數(shù),非10和10時(shí)的
5、輸出差異。3. 定義一個(gè)TimeSpan類,用TimeSpan.Add方法實(shí)現(xiàn)類中對象的加法,程序具體功能要求如下:TimeSpan類含一個(gè)總耗費(fèi)秒數(shù)變量,每小時(shí)秒數(shù)3600常量,每分鐘秒數(shù)60常量;構(gòu)造方法實(shí)現(xiàn)無參數(shù)時(shí)總耗秒為初設(shè)為0,具有小時(shí)、分鐘和秒?yún)?shù)時(shí)總耗秒為小時(shí)和分鐘及秒總含秒數(shù);打印出總共消耗小時(shí)數(shù)、分鐘數(shù)和秒數(shù);定義TimeSpan Add方法,實(shí)現(xiàn)兩個(gè)TimeSpan對象的加和;參考代碼如下:using System;class TimeSpan private uint totalSeconds; private const uint SecondsInHour = 360
6、0; private const uint SecondsInMinute = 60; public TimeSpan() totalSeconds = 0; public TimeSpan(uint initialHours, uint initialMinutes, uint initialSeconds) totalSeconds = initialHours * SecondsInHour + initialMinutes * SecondsInMinute + initialSeconds; public uint Seconds get return totalSeconds; s
7、et totalSeconds = value; public void PrintHourMinSec() uint hours; uint minutes; uint seconds; hours = totalSeconds / SecondsInHour; minutes = (totalSeconds % SecondsInHour) / SecondsInMinute; seconds = (totalSeconds % SecondsInHour) % SecondsInMinute; Console.WriteLine("0 Hours 1 Minutes 2 Sec
8、onds", hours, minutes, seconds); public static TimeSpan Add(TimeSpan timeSpan1, TimeSpan timeSpan2) TimeSpan sumTimeSpan = new TimeSpan(); sumTimeSpan.Seconds = timeSpan1.Seconds + timeSpan2.Seconds; return sumTimeSpan; class TimeSpanTest public static void Main() TimeSpan totalTime; TimeSpan m
9、yTime = new TimeSpan(1,20,30); TimeSpan yourTime = new TimeSpan(2,40,45); totalTime = TimeSpan.Add(myTime, yourTime); Console.Write("My time: "); myTime.PrintHourMinSec(); Console.Write("Your time: "); yourTime.PrintHourMinSec(); Console.Write("Total time: "); totalTime
10、.PrintHourMinSec(); 將以上程序的TimeSpanAdd方法改為加號操作符重載的實(shí)現(xiàn),并回答有何異同。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Test class TimeSpan private uint totalSeconds; private const uint SecondsInHour = 3600; private const uint SecondsInMinute = 60; public TimeSpan() t
11、otalSeconds = 0; public TimeSpan(uint initialHours, uint initialMinutes, uint initialSeconds) totalSeconds = initialHours * SecondsInHour + initialMinutes * SecondsInMinute + initialSeconds; public uint Seconds get return totalSeconds; set totalSeconds = value; public void PrintHourMinSec() uint hou
12、rs; uint minutes; uint seconds; hours = totalSeconds / SecondsInHour; minutes = (totalSeconds % SecondsInHour) / SecondsInMinute; seconds = (totalSeconds % SecondsInHour) % SecondsInMinute; Console.WriteLine("0 Hours 1 Minutes 2 Seconds", hours, minutes, seconds); public static TimeSpan op
13、erator +(TimeSpan timeSpan1, TimeSpan timeSpan2) TimeSpan sumTimeSpan = new TimeSpan(); sumTimeSpan.Seconds = timeSpan1.Seconds + timeSpan2.Seconds; return sumTimeSpan; class Program static void Main(string args) TimeSpan totalTime; TimeSpan myTime = new TimeSpan(1, 20, 30); TimeSpan yourTime = new
14、TimeSpan(2, 40, 45); totalTime = myTime + yourTime; Console.Write("My time: "); myTime.PrintHourMinSec(); Console.Write("Your time: "); yourTime.PrintHourMinSec(); Console.Write("Total time: "); totalTime.PrintHourMinSec(); Console.ReadLine(); 4. 多態(tài)程序練習(xí):基類shape類是一個(gè)表示形狀的
15、抽象類,area( )為求圖形面積的函數(shù)。請從shape類派生三角形類(triangle)、圓類(circles)、并給出具體的求面積函數(shù),并在主函數(shù)中多態(tài)地實(shí)現(xiàn)調(diào)用。using System;namespace Variables /public abstract class shape / / public abstract void MyArea(); / public class shape public virtual void MyArea() Console.WriteLine("no use"); public class circle : shape do
16、uble r,carea; public circle(double r) this.r = r; public override void MyArea() carea = Math.PI*r * r; Console.WriteLine("該圖形的面積為0",carea); public class triangle : shape double tarea,hemiline,h; public triangle(double hemiline,double h) this.hemiline = hemiline; this.h = h; public override
17、 void MyArea() tarea = hemiline * h / 2; Console.WriteLine("hemiline=0,h=1",hemiline,h); Console.WriteLine("該圖形的面積為0", tarea); public class Tester public static void Main() shape MyShape; double r = Convert.ToDouble(Console.ReadLine(); MyShape = new circle(r); MyShape.MyArea(); d
18、ouble h = Convert.ToDouble(Console.ReadLine(); double hemiline = Convert.ToDouble(Console.ReadLine(); MyShape = new triangle(hemiline, h); MyShape.MyArea(); Console.ReadLine(); 運(yùn)行結(jié)果:思考:(1) 將類shape定義為抽象類含有MyArea抽象方法再調(diào)試程序,查看效果。(2) 并回答抽象方法是否可以含 主體?(3) 如果將基類中虛方法關(guān)鍵字virtual去掉程序會怎樣?5. 覆蓋虛接口程序:以下程序組合了多種功能,請
19、參考如下代碼解釋并回答問題。l 該程序包含的類的個(gè)數(shù)和關(guān)系?l 類對接口的實(shí)現(xiàn)有何區(qū)別?“interface”(接口)關(guān)鍵字使抽象的概念更深入了一層。我們可將其想象為一個(gè)“純”抽象類。它允許創(chuàng)建者規(guī)定一個(gè)類的基本形式:方法名、自變量列表以及返回類型,但不規(guī)定方法主體。接口也包含了基本數(shù)據(jù)類型的數(shù)據(jù)成員,但它們都默認(rèn)為static和final。接口只提供一種形式,并不提供實(shí)施的細(xì)節(jié)。接口這樣描述自己:“對于實(shí)現(xiàn)我的所有類,看起來都應(yīng)該象我現(xiàn)在這個(gè)樣子”。因此,采用了一個(gè)特定接口的所有代碼都知道對于那個(gè)接口可能會調(diào)用什么方法。這便是接口的全部含義。所以我們常把接口用于建立類和類之間的一個(gè)“協(xié)議”。
20、有些面向?qū)ο蟮某绦蛟O(shè)計(jì)語言采用了一個(gè)名為“protocol”(協(xié)議)的關(guān)鍵字,它做的便是與接口相同的事情。為創(chuàng)建一個(gè)接口,請使用interface關(guān)鍵字,而不要用class。與類相似,我們可在interface關(guān)鍵字的前面增加一個(gè)public關(guān)鍵字(但只有接口定義于同名的一個(gè)文件內(nèi));或者將其省略,營造一種“友好的”狀態(tài)。為了生成與一個(gè)特定的接口(或一組接口)相符的類,要使用implements(實(shí)現(xiàn))關(guān)鍵字。我們要表達(dá)的意思是“接口看起來就象那個(gè)樣子,這兒是它具體的工作細(xì)節(jié)”。除這些之外,我們其他的工作都與繼承極為相似。l 第一個(gè)類中無參數(shù)構(gòu)造函數(shù)是否起作用,是否可以刪除不用?初始化tota
21、lSeconds,不能刪除。l 類中的屬性在哪里被應(yīng)用到?需要輸出的時(shí)候用到。Console.WriteLine(time.Seconds);l 第一個(gè)類中哪些成員被繼承,列出所有?totalSeconds l 第二個(gè)類中構(gòu)造方法成員如何實(shí)現(xiàn),有何意義?可以去掉么?調(diào)用基類的構(gòu)造函數(shù)。起到代碼重用的作用。public TimeSpanAdvanced(uint initialSeconds) : base(initialSeconds)l 第二個(gè)類覆蓋第一個(gè)類中接口虛方法,程序體現(xiàn)了什么功能區(qū)別?l Sorter類有何作用?你能否根據(jù)Sorter類寫一個(gè)十個(gè)數(shù)比較大小的冒泡法程序?l Sort
22、er類中for (int i = 0; swapped; i+)和 /for (int i = 0; i< bubbles.Length; i+)兩行是否作用相同?l 你知道Console.WriteLine("run outter");和Console.WriteLine("run inner");在程序運(yùn)行過程中可以起到什么作用?l 將Main方法中的TimeSpan對象語句(注釋掉的5行)和TimeSpanAdvanced對象語句選擇輪流注釋,體驗(yàn)排序結(jié)果的異同。l 語句Sorter.BubbleSortAscending(raceTimes
23、);前后的foreach語句功能區(qū)別。using System;public interface IComparable int CompareTo(IComparable comp);public class TimeSpan : IComparable private uint totalSeconds; public TimeSpan() totalSeconds = 0; public TimeSpan(uint initialSeconds) totalSeconds = initialSeconds; public uint Seconds get return totalSeco
24、nds; set totalSeconds = value; public virtual int CompareTo(IComparable comp) TimeSpan compareTime = (TimeSpan)comp; if (totalSeconds > compareTime.Seconds) return 1; else if (compareTime.Seconds = totalSeconds) return 0; else return -1; public class TimeSpanAdvanced : TimeSpan public TimeSpanAdv
25、anced(uint initialSeconds): base(initialSeconds) / public override int CompareTo(IComparable comp) TimeSpan compareTime = (TimeSpan)comp; if (base.Seconds > compareTime.Seconds) if (base.Seconds > (compareTime.Seconds + 50) return 2; else return 1; else if (base.Seconds < compareTime.Second
26、s) if (base.Seconds < (compareTime.Seconds - 50) return -2; else return -1; else return 0; class Sorter / Sort the comparable elements of an array in ascending order public static void BubbleSortAscending(IComparable bubbles) bool swapped = true; for (int i = 0; swapped; i+) /for (int i = 0; i<
27、; bubbles.Length; i+) Console.WriteLine("run outter"); swapped = false; for (int j = 0; j < (bubbles.Length - (i + 1); j+) if (bubblesj.CompareTo(bubblesj + 1) > 0) Console.WriteLine("run inner"); Swap(j, j + 1, bubbles); swapped = true; /Swap two elements of an array publi
28、c static void Swap(int first, int second, IComparable arr) IComparable temp; temp = arrfirst; arrfirst = arrsecond; arrsecond = temp; class Tester public static void Main() /TimeSpan raceTimes = new TimeSpan4; /raceTimes0 = new TimeSpan(153); /raceTimes1 = new TimeSpan(165); /raceTimes2 = new TimeSp
29、an(142); /raceTimes3 = new TimeSpan(108); TimeSpanAdvanced raceTimes = new TimeSpanAdvanced4; raceTimes0 = new TimeSpanAdvanced(153); raceTimes1 = new TimeSpanAdvanced(165); raceTimes2 = new TimeSpanAdvanced(142); raceTimes3 = new TimeSpanAdvanced(108); foreach (TimeSpan time in raceTimes) Console.W
30、riteLine(time.Seconds); Sorter.BubbleSortAscending(raceTimes); Console.WriteLine("List of sorted time spans:"); foreach (TimeSpan time in raceTimes) Console.WriteLine(time.Seconds); Console.ReadLine(); 運(yùn)行結(jié)果:6. 一個(gè)強(qiáng)大而復(fù)雜的銀行模擬程序:模擬一個(gè)持有若干銀行賬號的銀行,銀行帳戶可以通過控制臺窗口提供的一個(gè)簡單用戶界面來訪問和操作。用戶通過發(fā)出簡單命令必須能:開始指定
31、由銀行管理的賬戶數(shù);在指定帳戶上存款;從指定帳戶上提款;設(shè)置指定帳戶的利率;將利息加到所有帳戶上;打印帳戶結(jié)算;打印支付給每個(gè)帳戶的利息;打印每個(gè)帳戶的利率;結(jié)束模擬。軟件分析:確定兩個(gè)明顯的類:Account和Bank及將二者功能對應(yīng)的包含Main方法的BankSimulation;Account帳戶類包含實(shí)例變量:結(jié)算總額,當(dāng)前利率,總支付利息;另外帳戶類應(yīng)含有對帳戶結(jié)算增減、利率計(jì)算等的方法;所有實(shí)例變量在構(gòu)造函數(shù)中被初始化。Bank類的實(shí)例變量:一個(gè)帳戶數(shù)組,先要求輸入帳戶數(shù)組元素個(gè)數(shù);通過構(gòu)造方法初始化帳戶數(shù)組。其它涉及信息都可以在帳戶類生成的對象里獲得;因?yàn)锳ccount對象內(nèi)一般
32、實(shí)例變量為private,無法被外部訪問,所以Bank類要想訪問,可以用屬性或存取器、變異器方法。如currentInterestRate用setInterestRate和GetInterestRate來對當(dāng)前利率賦值和讀取,從而實(shí)現(xiàn)通過存取器對私有變量的外部訪問。Balance和totalInterestPaid也分別通過響應(yīng)的存取方法返回值。注意:設(shè)第一個(gè)帳戶account number為1,其對應(yīng)的數(shù)組索引為0,所以,Bank類中有accountsaccountNumber - 1的應(yīng)用。BankSimulation僅需要一個(gè)實(shí)例變量,一個(gè)Bank對象,其它對應(yīng)到前兩個(gè)類中;示例代碼如下
33、,閱讀后回答問題:using System;class Account private decimal balance; private decimal currentInterestRate; private decimal totalInterestPaid; public Account() balance = 0; currentInterestRate = 0; totalInterestPaid = 0; public void SetInterestRate(decimal newInterestRate) currentInterestRate = newInterestRat
34、e; public decimal GetInterestRate() return currentInterestRate; public void UpdateInterest() totalInterestPaid += balance * currentInterestRate; balance += balance * currentInterestRate; public void Withdraw (decimal amount) balance -= amount; public void Deposit (decimal amount) balance += amount;
35、public decimal GetBalance() return balance; public decimal GetTotalInterestPaid() return totalInterestPaid; class Bank private Account accounts; public Bank() Console.WriteLine("Congratulations! You have created a new bank"); Console.Write("Please enter number of accounts in bank: &qu
36、ot;); accounts = new AccountConvert.ToInt32(Console.ReadLine(); for (int i = 0; i < accounts.Length; i+) accountsi = new Account(); public void Deposit() int accountNumber; decimal amount; Console.Write("Deposit. Please enter account number: "); accountNumber = Convert.ToInt32(Console.R
37、eadLine(); Console.Write("Enter amount to deposit: "); amount = Convert.ToDecimal(Console.ReadLine(); accountsaccountNumber - 1.Deposit(amount); Console.WriteLine("New balance of account 0: 1:C", accountNumber, accountsaccountNumber - 1.GetBalance(); public void Withdraw() int ac
38、countNumber; decimal amount; Console.Write("Withdraw. Please enter account number: "); accountNumber = Convert.ToInt32(Console.ReadLine(); Console.Write("Enter amount to withdraw: "); amount = Convert.ToDecimal(Console.ReadLine(); accountsaccountNumber - 1.Withdraw(amount); Conso
39、le.WriteLine("New balance of account 0: 1:C", accountNumber, accountsaccountNumber - 1.GetBalance(); /注意,1:C這里代表本位置上輸出的數(shù)字以貨幣形式顯式。如$100. public void SetInterestRate() int accountNumber; decimal newInterestRate; Console.Write("Set interest rate. Please enter account number: "); acc
40、ountNumber = Convert.ToInt32(Console.ReadLine(); Console.Write("Enter interest rate: "); newInterestRate = Convert.ToDecimal(Console.ReadLine(); accountsaccountNumber - 1.SetInterestRate(newInterestRate); public void PrintAllInterestRates() Console.WriteLine("Interest rates for all ac
41、counts:"); for (int i = 0; i < accounts.Length; i+) Console.WriteLine("Account 0,-3: 1,-10", (i + 1), accountsi.GetInterestRate(); public void PrintAllBalances() Console.WriteLine("Account balances for all accounts:"); for (int i = 0; i < accounts.Length; i+) Console.W
42、riteLine("Account 0,-3: 1,12:C", (i + 1), accountsi.GetBalance(); public void PrintTotalInterestPaidAllAccounts() Console.WriteLine("Total interest paid for each individual account"); for (int i = 0; i < accounts.Length; i+) Console.WriteLine("Account 0,-3: 1,12:C",
43、(i + 1), accountsi.GetTotalInterestPaid(); public void UpdateInterestAllAccounts() for (int i = 0; i < accounts.Length; i+) Console.WriteLine("Interest added to account number 0,-3: 1,12:C", (i + 1), accountsi.GetBalance() * accountsi.GetInterestRate(); accountsi.UpdateInterest(); class
44、 BankSimulation private static Bank bigBucksBank; public static void Main() string command; bigBucksBank = new Bank(); do PrintMenu(); command = Console.ReadLine().ToUpper(); switch (command) case "D": bigBucksBank.Deposit(); break; case "W": bigBucksBank.Withdraw(); break; case "S": bigBucksBank.SetInterestRate(); break; case "U": bigBucksBank.UpdateInterestAllAccounts(); break; case "P": bigBucksBank.PrintAllBalances(); break; case "T": bigBucksBank.PrintT
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度城市景觀美化宣傳品制作合同3篇
- 應(yīng)急指揮系統(tǒng)的建設(shè)與優(yōu)化
- 電氣行業(yè)安全管理工作總結(jié)
- 二零二五年度花卉進(jìn)出口貿(mào)易合同協(xié)議3篇
- 二零二五年度個(gè)人二手房買賣風(fēng)險(xiǎn)評估合同2篇
- 二零二五年度個(gè)人醫(yī)療費(fèi)用收據(jù)模板定制合同3篇
- 二零二五版電力行業(yè)員工試用及轉(zhuǎn)正勞動合同范本3篇
- 2025版科研設(shè)備續(xù)租合同申請模板3篇
- 倉庫信息化流程
- 建筑行業(yè)工程師的工作總結(jié)
- 2024年山西文旅集團(tuán)招聘筆試參考題庫含答案解析
- 醫(yī)院定崗定編
- 恢復(fù)中華人民共和國國籍申請表
- 管理期貨的趨勢跟蹤策略 尋找危機(jī)阿爾法
- 瀝青化學(xué)分析試驗(yàn)作業(yè)指導(dǎo)書
- 2023年大學(xué)物理化學(xué)實(shí)驗(yàn)報(bào)告化學(xué)電池溫度系數(shù)的測定
- 腦出血的護(hù)理課件腦出血護(hù)理查房PPT
- 南京大學(xué)-大學(xué)計(jì)算機(jī)信息技術(shù)教程-指導(dǎo)書
- 扣繳個(gè)人所得稅報(bào)告表-(Excel版)
- 02R112 拱頂油罐圖集
- Unit+4+History+and+Traditions單元整體教學(xué)設(shè)計(jì)課件 高中英語人教版(2019)必修第二冊單元整體教學(xué)設(shè)計(jì)
評論
0/150
提交評論