Console.Write("{0,5:
d}",Y[i][j]);
Console.WriteLine();
}
}
}
实验三类和结构,接口委托与事件
【学时数:
4学时】
一、实验目的
1.熟悉C#类、对象、方法的概念与用法;
2.掌握编写简单的面向对象的应用程序;
3.熟悉并掌握C#构造方法及其重载方法的用法;
4.熟悉并掌握类和成员的访问修饰符的用法。
5.掌握抽象类与接口的定义及使用方法。
6.掌握抽象类与接口的定义及使用方法。
二、实验要求
1.编写一个Windows窗体应用程序,实现以下功能。
(1)定义一个CourseInfo累,该类包含以下成员。
●具有CourseName(课程名),CourseTime(开课时间),BookName(书名),Price(定价)4个属性,期中开课时间为枚举类型值(秋季,春季)。
●具有一个静态变量Counter,每创建一个Course实例,给变量值自动加1。
●提供无参数构造函数,在构造函数中设置相应的属性值。
●提供一个print方法,现实该实例的4个属性值。
(2)在主窗体的代码实现中,分别创建CourseInfo实例,测试类中提供的功能,并将结果显示出来。
2.单一继承:
用C#编写一个程序,使用Animal和Mammal两个类来说明一般动物和哺乳动物的继承关系。
Animal具有名称、所属门类等属性,需要提供方法实现以接收和显示这些属性的值。
Mammal类具有代表哺乳动物习性的属性,这些属性表明哺乳动物与其他类型动物的区别。
同样地,需要提供方法实现以接收和显示这些属性的值。
3.使用抽象类和抽象方法计算不同形状的面积:
编写一个程序,使用抽象类和抽象方法来计算不同形状的面积。
4.使用委托和事件实现模拟闹铃:
编写一个模拟闹铃的程序,具有闹铃,继续闹铃,打会盹儿,停止闹铃的功能。
(编写一个程序用来模拟闹钟闹起床流程,有一个闹钟类AlarmClock,催促起床类WakeMeUp,使用类AlarmDriver。
调用类引发开始闹铃事件,闹钟类调用委托方法进行处理,催促起床类和用户进行交互。
)
三、参考程序
四、参考程序
1.1.//*CourseInfo.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;namespaceClassTest
{
enumCourseTime{春季,秋季}
classCourseInfo
{
publicstaticintCounter=0;
///
///课程名
///
publicstringCourseName{get;set;}
///
///开课学期
///
publicCourseTimeCourseSemester;
///
///书名
///
publicstringBookName{get;set;}
///
///定价
///
publicdoublePrice{get;set;}
publicCourseInfo()
{
Counter++;
CourseName=BookName="";
CourseSemester=CourseTime.春季;
Price=0;
}
publicCourseInfo(stringcouseName,stringbookName,CourseTimecourseSemester,intprice)
{
Counter++;
CourseName=couseName;
BookName=bookName;
CourseSemester=courseSemester;
Price=price;
}
publicstringPrint()
{
returnstring.Format(
"{0,-10}{1,-10}{2,-10}{3,5}",
CourseName,
CourseSemester,
BookName,
Price);
}
}
}
//*Mainform.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceClassTest
{
publicpartialclassMainForm:
Form
{
publicMainForm()
{
InitializeComponent();
this.Load+=MainForm_Load;
}
voidMainForm_Load(objectsender,EventArgse)
{
listBox1.Items.Add(string.Format(
"{0,-10}{1,-10}{2,-10}{3,5}",
"课程名","开设学期","书名","定价"));
varcourse1=newCourseInfo
{
CourseName="数据结构",
CourseSemester=CourseTime.春季,
BookName="《数据结构》",
Price=40
};
listBox1.Items.Add(course1.Print());
varcourse2=newCourseInfo("操作系统","《操作系统》",CourseTime.秋季,45);
listBox1.Items.Add(course2.Print());
varcourse3=newCourseInfo("软件工程","《软件工程》",CourseTime.春季,38);
listBox1.Items.Add(course2.Print());
}
}
//*program.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceClassTest
{
staticclassProgram
{
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newMainForm());
}
}
}
2.usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceSingleInher
{
//一般动物类
classAnimal
{
protectedstring_name; //动物名称
protectedstring_type; //动物所属门类
//接收名称和门类
publicvoidAcceptDetails()
{
Console.WriteLine("请输入动物名称:
");
this._name=Console.ReadLine();
Console.WriteLine("请输入所属门类:
");
this._type=Console.ReadLine();
}
//显示动物的名称和所属门类
publicvoidDisplayDetails()
{
Console.WriteLine();
Console.WriteLine("{0}动物的详细信息如下:
",this._name);
Console.WriteLine("姓名:
{0}",this._name);
Console.WriteLine("门类:
{0}",this._type);
}
}
//哺乳动物类,继承一般动物类
classMammal:
Animal
{
privatedouble_temperature; //恒定温度值
privatestring_isViviparous; //是否为胎生
//接收哺乳动物的特征信息
publicvoidAcceptCharacter()
{
Console.WriteLine("请输入哺乳动物的恒定温度:
");
try
{
_temperature=Convert.ToDouble(Console.ReadLine());
}
catch//捕捉异常,例如:
若输入字母,在转换成Decimal时会引发异常
{
Console.WriteLine("请为温度输入数字!
");
}
Console.WriteLine("该哺乳动物是否胎生:
");
_isViviparous=Console.ReadLine();
}
//显示哺乳动物的特征信息
publicvoidDisplayCharacter()
{
Console.WriteLine();
Console.WriteLine("{0}的特征包括:
",this._name);
Console.WriteLine("恒定温度:
{0}",_temperature.ToString());
Console.WriteLine("是否胎生:
{0}",_isViviparous);
}
}
classProgram
{
staticvoidMain(string[]args)
{
MammalobjM=newMammal();
objM.AcceptDetails();//访问父类方法
objM.AcceptCharacter();
objM.DisplayDetails();//访问父类方法
objM.DisplayCharacter();
}
}
3.usingSystem;
abstractclassFigure//抽象类定义,表示一个抽象图形
{
protecteddoublex=0,y=0;
publicFigure(doublea,doubleb)
{x=a;y=b;}
publicabstractvoidArea(); //抽象方法,无实现代码,抽象图形无法计算面积
}
classSquare:
Figure //类Square定义,矩形类
{
publicSquare(doublea,doubleb)
:
base(a,b)
{}
publicoverridevoidArea() //不能使用new,必须用override
{Console.WriteLine("矩形面积是:
{0}",x*y);} //显示矩形面积
}
classCircle:
Figure //类Circle定义,圆类
{
publicCircle(doublea)
:
base(a,a)
{}
publicoverridevoidArea()
{Console.WriteLine("圆面积是:
{0}",3.14*x*y);} //显示圆的面积
}
classClass1
{
staticvoidMain(string[]args)
{
Squares=newSquare(20,30);
Circlec=newCircle(10);
s.Area();
c.Area();
}
}
4.usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespaceEventSample
{
//
//定义事件参数类
//
publicclassAlarmEventArgs:
EventArgs
{
privatereadonlyboolsnoozePressed;
privatereadonlyintnrings;
//构造函数
publicAlarmEventArgs(boolsnoozePressed,intnrings)
{
this.snoozePressed=snoozePressed;
this.nrings=nrings;
}
//定义属性,私有字段nrings
publicintNumRings
{
get{returnnrings;}
}
//定义属性,返回私有字段snoozePressed
publicboolSnoozePressed
{
get{returnsnoozePressed;}
}
//下面的属性