实验5c#.docx
《实验5c#.docx》由会员分享,可在线阅读,更多相关《实验5c#.docx(21页珍藏版)》请在冰豆网上搜索。
实验5c#
《C#程序设计B》实验报告
实验名称:
面向对象编程技术
实验日期:
2013.10.30
班级:
12512
学号:
20123051201
姓名:
焦玉莹
一、实验目的
1.熟练掌握用类创建对象的基本方法;
2.能根据需要正确设计类中的私有字段与公有方法;
3.理解多态性的概念及实现途径;
4.掌握虚方法的定义及实现;
5.掌握抽象方法的定义与用法。
二、实验内容
1.P107第8题
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_11
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatevoidbtnshezhi_Click(objectsender,EventArgse)
{
if(txtname.Text==""||txtnumber.Text==""||txtsex.Text=="")
{
MessageBox.Show("请输入完整信息");
}
if(txtname.Text!
=null&&txtnumber.Text!
=null&&txtsex.Text!
=null)
{
labresult.Text="对象值设置完毕:
";
}
}
privatevoidbtnxianshi_Click(objectsender,EventArgse)
{
Students=newStudent();
s.Number=txtnumber.Text;
s.Name=txtname.Text;
s.Sex=txtsex.Text;
labresult.Text="学号:
"+txtnumber.Text+""+"姓名:
"+txtname.Text+""+"性别:
"+txtsex.Text;
}
privatevoidtxtsex_TextChanged(objectsender,EventArgse)
{
if(txtsex.Text!
="女"&&txtsex.Text!
="男")
{
MessageBox.Show("请重新输入性别(_男或女)。
");
}
}
}
publicclassStudent
{
privatestringnumber;
privatestringname;
privatestringsex;
publicstringNumber
{
get{returnnumber;}
set{number=value;}
}
publicstringName
{
get{returnname;}
set{name=value;}
}
publicstringSex
{
get{returnsex;}
set{sex=value;}
}
}
}
2.创建Windows程序,如图所示。
通过主窗口可以对打开的新窗口执行关闭、显示、隐藏等操作;也可以通过主窗口设置新窗口的大小及其中标签的前景色与背景色。
图1程序初始界面
图2打开新窗口界面
图3设置新窗体属性界面
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_22
{
publicpartialclassForm1:
Form
{
Form2a=newForm2();
publicForm1()
{InitializeComponent();}
privatevoidForm1_Load(objectsender,EventArgse)
{}
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(radopen.Checked)
{a.Show();}
if(radclose.Checked)
{a.Close();}
if(radshow.Checked)
{a.Show();}
if(radhide.Checked)
{a.Hide();}
}
}
}
3.定义一个学生类CStudent,该类功能如下:
包含学生的姓名、性别、年龄、学号信息;在类中设置一个变量来记录共有多少个学生,即共为该类生成多少个实例;具有可输出生成实例个数的成员函数;具有可输入/输出学生信息的成员函数。
再派生出两个不同的类:
全日制学生和业余在职学生。
全日制学生添加监护人姓名及联系方式,业余在职学生添加工作单位及联系方式。
重载输入/输出成员函数以便实现输入/输出新添加的数据。
namespaceWindowsFormsApplication1
{
publicpartialclassForm1:
Form
{
publicForm1()
{InitializeComponent();}
CStudentcs=newCStudent();
intcount;
privatevoidForm1_Load(objectsender,EventArgse)
{}
privatevoidbtnok_Click(objectsender,EventArgse)
{
cs.name=txtname.Text;
cs.number=txtnumber.Text;
cs.sex=txtsex.Text;
cs.age=txtage.Text;
FullStudentfs=newFullStudent();
fs.guardername=txtjhname.Text;
fs.guarderphone=txtjhway.Text;
Spacetimestudentss=newSpacetimestudent();
ss.job=txtjob.Text;
ss.jobphone=txtjobway.Text;
count++;
}
privatevoidbtnoutput_Click(objectsender,EventArgse)
{
if(txtjhname.Text==""&&txtjhway.Text==""&&txtjob.Text==""&&txtjobway.Text=="")
{
labresult.Text="姓名:
"+txtname.Text+""+"学号:
"+txtnumber.Text+""+"性别?
"+txtsex.Text+""+"年龄:
"+txtage.Text+""+"学生的个数:
"+CStudent.StudentNum();
}
elseif(txtjob.Text==""||txtjobway.Text=="")
{
labresult.Text="姓名:
"+txtname.Text+""+"学号:
"+txtnumber.Text+""+"性别;"+txtsex.Text+""+"年龄:
"+txtage.Text+""+"学生的个数:
"+CStudent.StudentNum()+'\n'+"监护人姓名:
"+txtjhname.Text+""+"监护人联系方式:
"+txtjhway.Text;
}
elseif(txtjhname.Text==""||txtjhway.Text=="")
{
labresult.Text="姓名"+txtname.Text+""+"学号:
"+txtnumber.Text+""+"性别;"+txtsex.Text+""+"年龄:
"+txtage.Text+""+"学生的个数:
"+count+""+'\n'+"工作单位:
"+txtjob.Text+""+"工作单位联系方式;"+txtjobway.Text;
}
else
{
labresult.Text="姓名:
"+txtname.Text+""+"学号:
"+txtnumber.Text+""+"性别;"+txtsex.Text+""+"年龄:
"+txtage.Text+""+"学生的个数:
"+count+""+'\n'+"监¨护人姓名:
"+txtjhname.Text+""+"监护人联系方式:
"+""+'\n'+txtjhway.Text+"工作单位:
"+txtjob.Text+""+"工作单位联系方式:
"+txtjobway.Text;
}
}
privatevoidlabel3_Click(objectsender,EventArgse)
{
}
}
publicclassCStudent
{
publicstringname;
publicstringnumber;
publicstringsex;
publicstringage;
publicstaticintstudentnum;
publicstaticintStudentNum()
{returnstudentnum;}
publicCStudent()
{}
publicCStudent(stringna,stringnu,strings,stringa)
{
name=na;
number=nu;
sex=s;
age=a;
studentnum++;
}
}
publicclassFullStudent:
CStudent
{
publicstringguardername;
publicstringguarderphone;
publicFullStudent()
{}
publicFullStudent(stringna,stringnu,strings,stringa,stringgna,stringgp):
base()
{
guardername=gna;
guarderphone=gp;
}
}
publicclassSpacetimestudent:
CStudent
{
publicSpacetimestudent()
{}
publicstringjob;
publicstringjobphone;
publicSpacetimestudent(stringna,stringnu,strings,stringa,stringj,stringjp)
:
base()
{
job=j;
jobphone=jp;
}
}
}
4.编写程序,利用虚方法实现计算球、圆锥、圆柱的体积。
namespace第四题
{
publicpartialclassForm1:
Form
{
publicForm1()
{InitializeComponent();}
privatevoidbtnsphere_Click(objectsender,EventArgse)
{
labresult.Text="";
Spheres=newSphere();
s.r=double.Parse(txtr.Text);
labresult.Text+="球的体积为:
"+s.volume().ToString();
}
privatevoidbtncone_Click(objectsender,EventArgse)
{
labresult.Text="";
Conec=newCone();
c.r=double.Parse(txtr.Text);
c.h=double.Parse(txth.Text);
labresult.Text+="圆锥的体积为:
"+c.volume().ToString();
}
privatevoidbtncylinder_Click(objectsender,EventArgse)
{
labresult.Text="";
Cylindercy=newCylinder();
cy.r=double.Parse(txtr.Text);
cy.h=double.Parse(txth.Text);
labresult.Text+="圆柱的体积为:
"+cy.volume().ToString();
}
}
publicclassA
{
publicdoubler;
publicdoubleh;
publicvirtualdoublevolume()
{return0;}
}
publicclassSphere:
A
{
publicoverridedoublevolume()
{return4/3.0*r*r*r;}
}
publicclassCone:
A
{
publicoverridedoublevolume()
{return1/3.0*r*r*Math.PI*h;}
}
publicclassCylinder:
A
{
publicoverridedoublevolume()
{returnh*r*r*Math.PI;}
}
}
5.P124习题7.3第13题。
namespace第五题
{
publicpartialclassForm1:
Form
{
Student_1s1=newStudent_1();
publicForm1()
{InitializeComponent();}
privatevoidForm1_Load(objectsender,EventArgse)
{}
privatevoidlabel3_Click(objectsender,EventArgse)
{}
privatevoidbtnok_Click(objectsender,EventArgse)
{
s1.number=txtname.Text;
s1.name=txtname.Text;
s1.sex=txtsex.Text;
s1.age=int.Parse(txtage.Text);
s1.score1=double.Parse(txtscore1.Text);
s1.score2=double.Parse(txtscore2.Text);
doubletotal=double.Parse(txtscore1.Text)+double.Parse(txtscore2.Text);
doubleavg=total/2.0;
labresult.Text="学号:
"+txtnum.Text+"姓名:
"+txtname.Text+"性别:
"+txtsex.Text+"年龄:
"+txtage.Text+'\n'+"总分:
"+total+"平均分:
"+avg;
}
privatevoidbtnout_Click(objectsender,EventArgse)
{
txtnum.Text="";
txtname.Text="";
txtsex.Text="";
txtage.Text="";
txtscore1.Text="";
txtscore2.Text="";
labresult.Text="";
}
}
publicclassStudent
{
publicstringnumber;
publicstringname;
publicstringsex;
publicintage;
publicStudent()
{}
publicStudent(stringnu,stringna,strings,inta)
{
number=nu;
name=na;
sex=s;
age=a;
}
}
publicclassStudent_1:
Student
{
publicdoublescore1;
publicdoublescore2;
publicStudent_1()
{}
publicStudent_1(stringnu,stringna,strings,inta,doubles1,doubles2):
base(nu,na,s,a)
{score1=s1;
score2=s2;
}}}
6.P123习题7.3第14题。
namespace第六题
{
publicpartialclassForm1:
Form
{
publicForm1()
{InitializeComponent();}
privatevoidForm1_Load(objectsender,EventArgse)
{}
privatevoidbtncircle_Click(objectsender,EventArgse)
{
labresult.Text="";
Circlec=newCircle(double.Parse(txth.Text),0);
labresult.Text="圆2的Ì?
面?
积y:
êo"+'\n'+c.area();
}
privatevoidbtnrectangle_Click(objectsender,EventArgse)
{
labresult.Text="";
Rectangler=newRectangle(double.Parse(txth.Text),double.Parse(txtw.Text));
labresult.Text="矩?
形?
的Ì?
面?
积y:
êo"+'\n'+r.area();
}
privatevoidbtntrigon_Click(objectsender,EventArgse)
{
labresult.Text="";
Trigont=newTrigon(double.Parse(txth.Text),double.Parse(txtw.Text));
labresult.Text="三¨y角?
形?
的Ì?
面?
积y:
êo"+'\n'+t.area();
}
}
publicabstractclassGeometry
{
publicdoubleh;
publicdoublew;
publicGeometry(doublehh,doubleww)
{
h=hh;
w=ww;
}
publicabstractdoublearea();
}
publicclassCircle:
Geometry
{
publicCircle(doublehh,doubleww):
base(hh,ww)
{}
publicoverridedoublearea()
{
returnMath.PI*h*h;
}
}
publicclassRectangle:
Geometry
{
publicRectangle(doublehh,doubleww):
base(hh,ww)
{}
publicoverridedoublearea()
{returnh*w;}
}
publicclassTrigon:
Geometry
{
publicTrigon(doublehh,doubleww):
base(hh,ww)
{}
publicoverridedoublearea()
{return1/2.0*w*h;}
}
}
四、实验总结
1.在第二题中,显示窗口的大小与前景色、背景色不会弄。
2.定义派生类:
publicclass派生类名称:
基类名称{类体}
3.声明抽象类:
publicabstractclass类名称
{publicabstract返回类型方法名称(参数列表)}
4.声明抽象方法:
publicoverride返回类型方法名称(参数列表){}
5.基类声明虚方法:
publicvirtual方法名称(参数列表){}
6.派生类声明虚方法:
publicoverride方法名称(参数名称){}
7.派生类自然继承基类的成员,但不能继承