面向对象程序设计C#实验教案.docx

上传人:b****6 文档编号:16636878 上传时间:2023-07-15 格式:DOCX 页数:53 大小:29.32KB
下载 相关 举报
面向对象程序设计C#实验教案.docx_第1页
第1页 / 共53页
面向对象程序设计C#实验教案.docx_第2页
第2页 / 共53页
面向对象程序设计C#实验教案.docx_第3页
第3页 / 共53页
面向对象程序设计C#实验教案.docx_第4页
第4页 / 共53页
面向对象程序设计C#实验教案.docx_第5页
第5页 / 共53页
面向对象程序设计C#实验教案.docx_第6页
第6页 / 共53页
面向对象程序设计C#实验教案.docx_第7页
第7页 / 共53页
面向对象程序设计C#实验教案.docx_第8页
第8页 / 共53页
面向对象程序设计C#实验教案.docx_第9页
第9页 / 共53页
面向对象程序设计C#实验教案.docx_第10页
第10页 / 共53页
面向对象程序设计C#实验教案.docx_第11页
第11页 / 共53页
面向对象程序设计C#实验教案.docx_第12页
第12页 / 共53页
面向对象程序设计C#实验教案.docx_第13页
第13页 / 共53页
面向对象程序设计C#实验教案.docx_第14页
第14页 / 共53页
面向对象程序设计C#实验教案.docx_第15页
第15页 / 共53页
面向对象程序设计C#实验教案.docx_第16页
第16页 / 共53页
面向对象程序设计C#实验教案.docx_第17页
第17页 / 共53页
面向对象程序设计C#实验教案.docx_第18页
第18页 / 共53页
面向对象程序设计C#实验教案.docx_第19页
第19页 / 共53页
面向对象程序设计C#实验教案.docx_第20页
第20页 / 共53页
亲,该文档总共53页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

面向对象程序设计C#实验教案.docx

《面向对象程序设计C#实验教案.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计C#实验教案.docx(53页珍藏版)》请在冰点文库上搜索。

面向对象程序设计C#实验教案.docx

面向对象程序设计C#实验教案

课程教学日历

课程名称:

面向对象的程序设计(C#)学期:

2014-2015-1

周次

章节及教学内容

累计学时

2

实验一熟悉VS开发环境

2

4

实验二基本数据类型和流程控制语句

2

6,8

实验三类和结构,接口委托与事件

4

10

实验四泛型与LINQ与文件操作

2

12,14

实验五WPF应用程序及WPF控件

4

16

实验六WPF样式及数据验证和数据库应用

2

实验一熟悉VS开发环境

【学时数:

2学时】

一、实验目的

1.掌握VisualStudio.NET2012的基本操作方法。

2.掌握C#控制台应用程序、Windows应用程序的基本操作过程。

3.初步理解C#程序的结构和特点,掌握基本的编译和调试方法。

二、实验内容

1.熟悉VisualStudio.NET2012集成开发环境的使用。

启动VS2012,熟悉它的基本界面。

建立源程序文件,输入、编译程序文件。

构建可执行文件。

运行程序,查看结果

2.设计一个简单的C#控制台应用程序,逐行显示自己的学号、姓名、专业信息。

(参考教材P5页例)

3.设计一个Windows应用程序,在窗体上显示三个标签框,标签框里的内容分别为自己的学号、姓名、专业。

(参考教材P20页例1-4)

4.阅读教材并录入教材例1-2,例1-5,例1-6程序,运行并输出结果,了解C#窗体应用程序的创建。

并思考:

(1)namespace关键字声明命名空间有何作用?

using关键字指明引用的命名空间有何作用?

三、实验报告

完成本次实验的报告,写清实验目的和要求、实验内容和实验结果。

实验二基本数据类型和流程控制语句

【学时数:

2学时】

一、实验目的

1.理解C#的值类型、常量和变量的概念。

2.掌握理解数据类型的转换以及数据类型的转换用法;

3.掌握C#的常用运算符以及表达式的运算规则。

4.掌握C#的两种分支语句(if、switch)的使用方法。

5.掌握C#的4种循环语句(while、do/while、for、foreach)的使用方法。

6.掌握引用类型中的字符串、数组的使用。

二、实验内容

1.设计一个控制台程序,从键盘输入一个人的名字,在显示器中显示对输入姓名的问候。

比如输入:

张三;输出:

张三,欢迎你!

提示:

输入字符串放入s中,将s+”,欢迎你!

”,再输出。

2.编写一个控制台程序,输入一个学生的成绩,输出其等级。

(优:

>=90;良:

>=80;中:

>=70;及格:

>=60;不及格:

<60)。

分别用if语句及switch-case语句实现。

3.编写一个控制台应用程序,求100之内的所有“素数”。

4.打印输出杨辉三角形。

三、实验报告

完成本次实验的报告,写清实验目的和要求、实验内容和实验结果。

四、参考程序

1./*programe2-1*/

usingSystem;

namespacep21

{

classprogram

{

staticvoidMain()

{

Console.Write("pleaseinteraperson'sname");

strings;

s=Console.ReadLine();

Console.WriteLine("s+欢迎你!

");

}

}

}

2.usingSystem;

namespacep23

{

classprogram

{

staticvoidMain()

{

Console.Write("pleaseinteranumbertox");

intx;

x=int.parse(Console.ReadLine());

if(x>=90)

Console.WriteLine("优秀!

");

elseif(x>=80)

Console.WriteLine("良!

");

elseif(x>=70)

Console.WriteLine("中!

");

elseif(x>=60)

Console.WriteLine("及格!

");

else

Console.WriteLine("差!

");

}

}

}

usingSystem;

namespacep23

{

classprogram

{

staticvoidMain()

{

Console.Write("pleaseinteranumbertox");

intx;

x=int.parse(Console.ReadLine());

inti=x/10;

switch(i)

{case10:

case9:

Console.WriteLine("优秀!

");

break;

case8:

Console.WriteLine("良!

");

break;

case7:

Console.WriteLine("中!

");break;

case6:

Console.WriteLine("及格!

");break;

default:

Console.WriteLine("差!

");break;

}

}

}

3.usingSystem;

namespacep23

{

classprogram

{

publicstaticboolprime(intm)

{

for(ingi=2;i

if(m%i==0)

returnfalse;

returntrue;

}

publicstaticvoidMain()

{

intm,k,n=1;

Console.Write("{0,-4}",2);

for(m=3;m<100;m+=2)

{

if(prime(m);

{

Console.Write("{0,-4}",m);

if(++n%10==0)

Console.WriteLine("\n");

}

}

}

}

4.

usingSystem;

classp24

{

publicstaticvoidMain()

{

inti,j,k,m;

k=7;

int[][]Y=newint[k][];//定义二维锯齿状数组

for(i=0;i

{

Y[i]=newint[i+1];

Y[i][0]=1;

Y[i][i]=1;

}

for(i=2;i

for(j=1;j

Y[i][j]=Y[i-1][j-1]+Y[i-1][j];

for(i=0;i

{

for(j=0;j

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;}

       }

       //下面的属性

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2