




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
高级C#开发技术习题1.用enum定义字节类型的方位常量,打印出某一方位并将此方位值转化为字节类型,字符串类型值。分析输出结果的原因。回答以下问题:Enum的缺省类型是什么?回答:Enum在C#中是一种值类型(Value Type),其基类型必须是整数类型(如Int16)直接输出myDirection和(byte)myDirection有何区别。回答:这是符号名和常数值的互相转换,当枚举变量转换为常数值时,必须使用强制转换。class Variables enum orientation : byte north = 1, south = 2, east = 3, west = 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 equivalent = 0, directionByte); Console.WriteLine(string equivalent = 0, directionString);Console.ReadLine(); 2建立使用关系运算符和逻辑运算符的程序文件。Main方法中实例代码如下static void Main(string args) Console.WriteLine(Enter an integer:); int myInt = Convert.ToInt32(Console.ReadLine(); Console.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(); 编译运行该程序。并尝试myInt输入不同范围整数,非10和10时的输出差异。3. 定义一个TimeSpan类,用TimeSpan.Add方法实现类中对象的加法,程序具体功能要求如下:TimeSpan类含一个总耗费秒数变量,每小时秒数3600常量,每分钟秒数60常量;构造方法实现无参数时总耗秒为初设为0,具有小时、分钟和秒参数时总耗秒为小时和分钟及秒总含秒数;打印出总共消耗小时数、分钟数和秒数;定义TimeSpan Add方法,实现两个TimeSpan对象的加和;参考代码如下:using System;class TimeSpan private uint totalSeconds; private const uint SecondsInHour = 3600; 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; set 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 Seconds, 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 myTime = 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.PrintHourMinSec(); 将以上程序的TimeSpanAdd方法改为加号操作符重载的实现,并回答有何异同。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() totalSeconds = 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 hours; 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 operator +(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 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. 多态程序练习:基类shape类是一个表示形状的抽象类,area( )为求图形面积的函数。请从shape类派生三角形类(triangle)、圆类(circles)、并给出具体的求面积函数,并在主函数中多态地实现调用。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 double 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 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(); double h = Convert.ToDouble(Console.ReadLine(); double hemiline = Convert.ToDouble(Console.ReadLine(); MyShape = new triangle(hemiline, h); MyShape.MyArea(); Console.ReadLine(); 运行结果:思考:(1) 将类shape定义为抽象类含有MyArea抽象方法再调试程序,查看效果。(2) 并回答抽象方法是否可以含 主体?(3) 如果将基类中虚方法关键字virtual去掉程序会怎样?5. 覆盖虚接口程序:以下程序组合了多种功能,请参考如下代码解释并回答问题。l 该程序包含的类的个数和关系?l 类对接口的实现有何区别?“interface”(接口)关键字使抽象的概念更深入了一层。我们可将其想象为一个“纯”抽象类。它允许创建者规定一个类的基本形式:方法名、自变量列表以及返回类型,但不规定方法主体。接口也包含了基本数据类型的数据成员,但它们都默认为static和final。接口只提供一种形式,并不提供实施的细节。接口这样描述自己:“对于实现我的所有类,看起来都应该象我现在这个样子”。因此,采用了一个特定接口的所有代码都知道对于那个接口可能会调用什么方法。这便是接口的全部含义。所以我们常把接口用于建立类和类之间的一个“协议”。有些面向对象的程序设计语言采用了一个名为“protocol”(协议)的关键字,它做的便是与接口相同的事情。为创建一个接口,请使用interface关键字,而不要用class。与类相似,我们可在interface关键字的前面增加一个public关键字(但只有接口定义于同名的一个文件内);或者将其省略,营造一种“友好的”状态。为了生成与一个特定的接口(或一组接口)相符的类,要使用implements(实现)关键字。我们要表达的意思是“接口看起来就象那个样子,这儿是它具体的工作细节”。除这些之外,我们其他的工作都与继承极为相似。l 第一个类中无参数构造函数是否起作用,是否可以删除不用?初始化totalSeconds,不能删除。l 类中的属性在哪里被应用到?需要输出的时候用到。Console.WriteLine(time.Seconds);l 第一个类中哪些成员被继承,列出所有?totalSeconds l 第二个类中构造方法成员如何实现,有何意义?可以去掉么?调用基类的构造函数。起到代码重用的作用。public TimeSpanAdvanced(uint initialSeconds) : base(initialSeconds)l 第二个类覆盖第一个类中接口虚方法,程序体现了什么功能区别?l Sorter类有何作用?你能否根据Sorter类写一个十个数比较大小的冒泡法程序?l Sorter类中for (int i = 0; swapped; i+)和 /for (int i = 0; i compareTime.Seconds) return 1; else if (compareTime.Seconds = totalSeconds) return 0; else return -1; public class TimeSpanAdvanced : TimeSpan public TimeSpanAdvanced(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.Seconds) 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 bubbles.Length; i+) Console.WriteLine(run outter); swapped = false; for (int j = 0; j 0) Console.WriteLine(run inner); Swap(j, j + 1, bubbles); swapped = true; /Swap two elements of an array public 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 TimeSpan(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.WriteLine(time.Seconds); Sorter.BubbleSortAscending(raceTimes); Console.WriteLine(List of sorted time spans:); foreach (TimeSpan time in raceTimes) Console.WriteLine(time.Seconds); Console.ReadLine(); 运行结果:6. 一个强大而复杂的银行模拟程序:模拟一个持有若干银行账号的银行,银行帐户可以通过控制台窗口提供的一个简单用户界面来访问和操作。用户通过发出简单命令必须能:开始指定由银行管理的账户数;在指定帐户上存款;从指定帐户上提款;设置指定帐户的利率;将利息加到所有帐户上;打印帐户结算;打印支付给每个帐户的利息;打印每个帐户的利率;结束模拟。软件分析:确定两个明显的类:Account和Bank及将二者功能对应的包含Main方法的BankSimulation;Account帐户类包含实例变量:结算总额,当前利率,总支付利息;另外帐户类应含有对帐户结算增减、利率计算等的方法;所有实例变量在构造函数中被初始化。Bank类的实例变量:一个帐户数组,先要求输入帐户数组元素个数;通过构造方法初始化帐户数组。其它涉及信息都可以在帐户类生成的对象里获得;因为Account对象内一般实例变量为private,无法被外部访问,所以Bank类要想访问,可以用属性或存取器、变异器方法。如currentInterestRate用setInterestRate和GetInterestRate来对当前利率赋值和读取,从而实现通过存取器对私有变量的外部访问。Balance和totalInterestPaid也分别通过响应的存取方法返回值。注意:设第一个帐户account number为1,其对应的数组索引为0,所以,Bank类中有accountsaccountNumber - 1的应用。BankSimulation仅需要一个实例变量,一个Bank对象,其它对应到前两个类中;示例代码如下,阅读后回答问题: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 = newInterestRate; 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; 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: ); 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.ReadLine(); 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 accountNumber; 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); Console.WriteLine(New balance of account 0: 1:C, accountNumber, accountsaccountNumber - 1.GetBalance(); /注意,1:C这里代表本位置上输出的数字以货币形式显式。如$100. public void SetInterestRate() int accountNumber; decimal newInterestRate; Console.Write(Set interest rate. Please enter account number: ); accountNumber = 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 accounts:); 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.WriteLine(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, (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 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: bigB
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 学校年级团建活动方案策划
- 工地建筑围挡搭建方案设计
- 咨询目标方案
- 游泳馆营销活动方案六一
- 大洼装修方案咨询热线
- 建筑师方案设计作图
- 社团活动策划创意方案
- 媒体广告行业市场竞争环境研究报告
- 企业运营效率提升措施方案框架
- 2025导游资格考试能力提升B卷题库附参考答案详解(基础题)
- 2025年贵州省凯里市辅警招聘考试题题库(含参考答案)
- 大数据产业课件
- 潮汐能发电站课件
- 国际化跨国经营中的伦理问题概述
- 2025-2026学年度武汉市部分学校高三年级九月调研考试 语文试卷(含标准答案)
- 2025年禁毒知识竞赛试题及参考答案
- 初二入团考试内容及答案
- 2025至2030年中国交通节能服务行业发展潜力分析及投资战略咨询报告
- 针灸科感控知识培训课件
- 微生物学讲课文档
- 2024新版2025秋人教版二年级艺术造型美术上册全册教案教学设计(含大单元教学设计)
评论
0/150
提交评论