-1-高级C#开发技术习题1.用enum定义字节类型的方位常量,打印出某一方位并将此方位值转化为字节类型,字符串类型值。分析输出结果的原因。回答以下问题:Enum的缺省类型是什么?回答:Enum在C#中是一种值类型(ValueType),其基类型必须是整数类型(如Int16)直接输出myDirection和(byte)myDirection有何区别。回答:这是符号名和常数值的互相转换,当枚举变量转换为常数值时,必须使用强制转换。classVariables{enumorientation:byte{north=1,south=2,east=3,west=4}staticvoidMain(string[]args){bytedirectionByte;stringdirectionString;orientationmyDirection=orientation.north;Console.WriteLine(myDirection={0},myDirection);directionByte=(byte)myDirection;directionString=Convert.ToString(myDirection);Console.WriteLine(byteequivalent={0},directionByte);Console.WriteLine(stringequivalent={0},directionString);Console.ReadLine();}}2.建立使用关系运算符和逻辑运算符的程序文件。Main方法中实例代码如下staticvoidMain(string[]args){Console.WriteLine(Enteraninteger:);intmyInt=Convert.ToInt32(Console.ReadLine());Console.WriteLine(Integerlessthan10?{0},myInt10);Console.WriteLine(Integerbetween0and5?{0},(0=myInt)&&(myInt=5));Console.WriteLine(BitwiseANDofIntegerand10={0},myInt&10);Console.ReadLine();}编译运行该程序。并尝试myInt输入不同范围整数,非10和10时的输出差异。3.定义一个TimeSpan类,用TimeSpan.Add方法实现类中对象的加法,程序具体功能要求如下:TimeSpan类含一个总耗费秒数变量,每小时秒数3600常量,每分钟秒数60常量;构造方法实现无参数时总耗秒为初设为0,具有小时、分钟和秒参数时总耗秒为小时和分钟及秒总含秒数;打印出总共消耗小时数、分钟数和秒数;定义TimeSpanAdd方法,实现两个TimeSpan对象的加和;参考代码如下:usingSystem;-2-classTimeSpan{privateuinttotalSeconds;privateconstuintSecondsInHour=3600;privateconstuintSecondsInMinute=60;publicTimeSpan(){totalSeconds=0;}publicTimeSpan(uintinitialHours,uintinitialMinutes,uintinitialSeconds){totalSeconds=initialHours*SecondsInHour+initialMinutes*SecondsInMinute+initialSeconds;}publicuintSeconds{get{returntotalSeconds;}set{totalSeconds=value;}}publicvoidPrintHourMinSec(){uinthours;uintminutes;uintseconds;hours=totalSeconds/SecondsInHour;minutes=(totalSeconds%SecondsInHour)/SecondsInMinute;seconds=(totalSeconds%SecondsInHour)%SecondsInMinute;Console.WriteLine({0}Hours{1}Minutes{2}Seconds,hours,minutes,seconds);}publicstaticTimeSpanAdd(TimeSpantimeSpan1,TimeSpantimeSpan2){TimeSpansumTimeSpan=newTimeSpan();sumTimeSpan.Seconds=timeSpan1.Seconds+timeSpan2.Seconds;returnsumTimeSpan;}}classTimeSpanTest{publicstaticvoidMain(){TimeSpantotalTime;TimeSpanmyTime=newTimeSpan(1,20,30);TimeSpanyourTime=newTimeSpan(2,40,45);totalTime=TimeSpan.Add(myTime,yourTime);Console.Write(Mytime:);myTime.PrintHourMinSec();Console.Write(Yourtime:);yourTime.PrintHourMinSec();Console.Write(Totaltime:);totalTime.PrintHourMinSec();}}将以上程序的TimeSpanAdd方法改为加号操作符重载的实现,并回答有何异同。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceTest-3-{classTimeSpan{privateuinttotalSeconds;privateconstuintSecondsInHour=3600;privateconstuintSecondsInMinute=60;publicTimeSpan(){totalSeconds=0;}publicTimeSpan(uintinitialHours,uintinitialMinutes,uintinitialSeconds){totalSeconds=initialHours*SecondsInHour+initialMinutes*SecondsInMinute+initialSeconds;}publicuintSeconds{get{returntotalSeconds;}set{totalSeconds=value;}}publicvoidPrintHourMinSec(){uinthours;uintminutes;uintseconds;hours=totalSeconds/SecondsInHour;minutes=(totalSeconds%SecondsInHour)/SecondsInMinute;seconds=(totalSeconds%SecondsInHour)%SecondsInMinute;Console.WriteLine({0}Hours{1}Minutes{2}Seconds,hours,minutes,seconds);}publicstaticTimeSpanoperator+(TimeSpantimeSpan1,TimeSpantimeSpan2){TimeSpansumTimeSpan=newTimeSpan();sumTimeSpan.Seconds=timeSpan1.Seconds+timeSpan2.Seconds;returnsumTimeSpan;}}classProgram{-4-staticvoidMain(string[]args){TimeSpantotalTime;TimeSpanmyTime=newTimeSpan(1,20,30);TimeSpanyourTime=newTimeSpan(2,40,45);totalTime=myTime+yourTime;Console.Write(Mytime:);myTime.PrintHourMinSec();Console.Write(Yourtime:);yourTime.PrintHourMinSec();Console.Write(Totaltime:);totalTime.PrintHourMinSec();Console.ReadLine();}}}4.多态程序练习:基类shape类是一个表示形状的抽象类,area()为求图形面积的函数。请从shape类派生三角形类(triangle)、圆类(circles)、并给出具体的求面积函数,并在主函数中多态地实现调用。usingSystem;namespaceVariables{//publicabstractclassshape//{//publicabstractvoidMyArea();//}publicclassshape{publicvirtualvoidMyArea(){Console.WriteLine(nouse);}}publicclasscircle:shape{doubler,carea;publiccircle(doubler){this.r=r;}publicoverridevoidMyArea(){carea=Math.PI*r*r;Console.WriteLine(该图形的面积为{0},carea);}}publicclasstriangle:shape{doubletarea,hemiline,h;publictriangle(doublehemiline,doubleh){this.hemiline=hemiline;this.h=h;}publicoverridevoidMyArea(){tarea=hemiline*h/2;Console.WriteLine(hemiline={0},h={1},hemiline,h);Console.WriteLine(该图形的面积为{0},tarea);}}publicclassTester{publicstaticvoidMain(){shapeMyShape;doubler=Convert.ToDouble(Console.ReadLine());MyShape=newcircle(r);MyShape.MyArea();doubleh=Convert.ToDouble(Console.ReadLine());doublehemiline=Convert.ToDouble(Console.ReadLine());MyShape=newtriangle(hemiline,h);MyShape.MyArea();Console.ReadLine();}}}运行结果:-5-思考:(1)将类shape定义为抽象类含有MyArea抽象方法再调试程序,查看效果。(2)并回答抽象方法是否可以含{}主体?-6-(3)如果将基类中虚方法关键字virtual去掉程序会怎样?5.覆盖虚接口程序:以下程序组合了多种功能,请参考如下代码解释并回答问题。该程序包含的类的个数和关系?类对接口的实现有何区别?“interface”(接口)关键字使抽象的概念更深入了一层。我们可将其想象为一个“纯”抽象类。它允许创建者规定一个类的基本形式:方法名、自变量列表以及返回类型,但不规定方法主体。接口也包含了基本数据类型的数据成员,但它们都默认为static和final。接口只提供一种形式,并不提供实施的细节。-7-接口这样描述自己:“对于实现我的所有类,看起来都应该象我现在这个样子”