C#程序题更新.docx
《C#程序题更新.docx》由会员分享,可在线阅读,更多相关《C#程序题更新.docx(10页珍藏版)》请在冰豆网上搜索。
![C#程序题更新.docx](https://file1.bdocx.com/fileroot1/2023-2/26/a950eb65-c776-4ab4-a94a-47395a5f113d/a950eb65-c776-4ab4-a94a-47395a5f113d1.gif)
C#程序题更新
大家参看下面代码,在考试的时候解释用的xml代码<
///
///定义接口IShape
///
)不必写上!
这个代码是我写的。
同学看了题目以后觉得代码写的不好的可以重先写来与大家分享。
我觉得第二题的代码写得不太好<我觉得本身题目就有点问题),有时间的同学可以改写,与大家分享!
!
!
b5E2RGbCAP
1.定义接口IShape和两个类Circle<圆)和Square<矩形),实现图形面积
p1EanqFDPw
staticvoidMain(string[]args>
{
CirclecirObj=newCircle(1d>。
SquaresquObj=newSquare(2d,3d>。
Console.WriteLine("圆的面积:
{0},圆的周长:
{1}",cirObj.Area(>,cirObj.Circumference(>>。
DXDiTa9E3d
Console.WriteLine("长方形的面积:
{0},长方形的周长:
{1}",squObj.Area(>,squObj.Circumference(>>。
RTCrpUDGiT
}
///
///定义接口IShape
///
interfaceIShape
{
doubleArea(>。
doubleCircumference(>。
}
///
///定义圆类,实现IShape接口
///
publicclassCircle:
IShape
{
//半径
privatedoubleradius。
///
///带参构造函数
///
///半径
publicCircle(doubleradius>
{
this.radius=radius。
}
///
///求面积
///
///面积
publicdoubleArea(>
{
returnMath.PI*radius*radius。
}
///
///求周长
///
///周长
publicdoubleCircumference(>
{
returnMath.PI*2*radius。
}
}
///
///定义长方形类,实现IShape接口
///
publicclassSquare:
IShape
{
privatedoublewidth。
//宽
privatedoubleheight。
//高
///
///带参构造函数
///
///宽
///高
publicSquare(doublewidth,doubleheight>
{
this.width=width。
this.height=height。
}
///
///求面积
///
///面积
publicdoubleArea(>
{
returnwidth*height。
}
///
///求周长
///
///周长
publicdoubleCircumference(>
{
return2*(width+height>。
}
}
2.编写一个完整的程序。
该程序包含类Person、Student、Test,具体要求如下:
<1)类Person
①属性
name:
String类型,表示一个人的姓名
sex:
Boolean类型,用来表示性别
id:
String类型,表示身份证号
②方法
Person(nameAsString,sexAsBoolean,idAsString>:
构造函数5PCzVD7HxA
publicFunctiontoString(>AsString:
返回个人的各项信息,包括姓名、性别等上述属性jLBHrnAILg
<2)类Student
从Person类派生,增加了以下属性和方法:
①属性
sNo:
String类型,表示学生的学号
sClass:
String类型,表示学生的班级
②方法
Student(sNoAsString,nameAsString,sexAsBoolean,idAsstring,sClassAsString>:
构造函数xHAQX74J0X
publicFunctiontoString(>AsString:
返回学生的各项信息,包括学号、班级、姓名等上述属性LDAYtRyKfE
<3)类Test完成测试功能
publicclassPerson
{
protectedStringname。
protectedBooleansex。
protectedStringid。
publicPerson(>
{}
publicPerson(Stringname,Booleansex,Stringid>
{
this.name=name。
this.sex=sex。
this.id=id。
}
publicvirtualstringtoString(>
{
stringSex=sex?
"男":
"女"。
return"姓名:
"+name+",性别:
"+Sex+",身份证:
"+id+""。
Zzz6ZB2Ltk
}
}
publicclassStudent:
Person
{
privateStringsNo。
privateStringsClass。
publicStudent(StringsNo,Stringname,Booleansex,Stringid,StringsClass>dvzfvkwMI1
{
this.name=name。
this.sex=sex。
this.id=id。
this.sNo=sNo。
this.sClass=sClass。
}
publicoverridestringtoString(>
{
stringSex=sex?
"男":
"女"。
return"学号:
"+sNo+",姓名:
"+name+",性别:
"+Sex+",身份证:
"+id+",班级:
"+sClass+""。
rqyn14ZNXI
}
}
publicclassTest
{
staticvoidMain(string[]args>
{
PersonstuObj。
stuObj=newPerson("ls",true,"010102">。
Console.WriteLine(stuObj.toString(>>。
stuObj=newStudent("01","zs",true,"01010","08网络">。
EmxvxOtOco
Console.WriteLine(stuObj.toString(>>。
}
}
在Student类中构造函数写成publicStudent(StringsNo,Stringname,Booleansex,Stringid,StringsClass>:
base(name,sex,id>这样的话,就可以省去被注释掉的那部分代码方法中也可以省去一些代码!
SixE2yXPq5
<注意:
也就是说在下面的代码中绿色的部分是不需要的,比上面的程序省去了几行代码,也更能体现继承的作用,同时大家也方便我们记忆)6ewMyirQFL
#region第二题
publicclassPerson
{
protectedStringname。
protectedBooleansex。
protectedStringid。
//publicPerson(>
//{}
publicPerson(Stringname,Booleansex,Stringid>
{
this.name=name。
this.sex=sex。
this.id=id。
}
publicvirtualstringtoString(>
{
stringSex=sex?
"男":
"女"。
return"姓名:
"+name+",性别:
"+Sex+",身份证:
"+id+""。
kavU42VRUs
}
}
publicclassStudent:
Person
{
privateStringsNo。
privateStringsClass。
publicStudent(StringsNo,Stringname,Booleansex,Stringid,StringsClass>:
base(name,sex,id>y6v3ALoS89
{//this.name=name。
//this.sex=sex。
//this.id=id。
this.sNo=sNo。
this.sClass=sClass。
}
publicoverridestringtoString(>
{
//stringSex=sex?
"男":
"女"。
returnbase.toString(>+",学号:
"+sNo+",班级:
"+sClass。
M2ub6vSTnP
//return"学号:
"+sNo+",姓名:
"+name+",性别:
"+Sex+",身份证:
"+id+",班级:
"+sClass+""。
0YujCfmUCw
}
}
publicclassTest
{
staticvoidMain(string[]args>
{
PersonstuObj。
stuObj=newPerson("ls",true,"010102">。
Console.WriteLine(stuObj.toString(>>。
stuObj=newStudent("01","zs",true,"010101","08网络">。
eUts8ZQVRd
Console.WriteLine(stuObj.toString(>>。
}
}
#endregion
申明:
所有资料为本人收集整理,仅限个人学习使用,勿做商业用途。