面向對象的程序設計C#實驗教案_第1頁
面向對象的程序設計C#實驗教案_第2頁
面向對象的程序設計C#實驗教案_第3頁
面向對象的程序設計C#實驗教案_第4頁
面向對象的程序設計C#實驗教案_第5頁
已閱讀5頁,還剩30頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、課 程 教 學 日 歷課程名稱: 面向對象的程序設計(C#) 學期:2014-2015-1周次章節(jié)及教學內(nèi)容累計學時2實驗一 熟悉V S開發(fā)環(huán)境24實驗二 基本數(shù)據(jù)類型和流程控制語句26,8實驗三 類和結構,接口委托與事件410實驗四 泛型與LINQ與文件操作212,14實驗五 WPF應用程序及WPF控件416實驗六 WPF樣式及數(shù)據(jù)驗證和數(shù)據(jù)庫應用2實驗一 熟悉VS開發(fā)環(huán)境【學時數(shù):2學時】一、實驗目的1. 掌握Visual Studio.NET2012的基本操作方法。2. 掌握#控制臺應用程序、Windows應用程序的基本操作過程。3. 初步理解#程序的結構和特點,掌握基本的編譯和調(diào)試方法

2、。二、實驗內(nèi)容1. 熟悉Visual Studio.NET2012集成開發(fā)環(huán)境的使用。啟動VS2012,熟悉它的基本界面。建立源程序文件,輸入、編譯程序文件。構建可執(zhí)行文件。運行程序,查看結果2. 設計一個簡單的#控制臺應用程序,逐行顯示自己的學號、姓名、專業(yè)信息。(參考教材P5頁例)3. 設計一個Windows應用程序,在窗體上顯示三個標簽框,標簽框里的內(nèi)容分別為自己的學號、姓名、專業(yè)。(參考教材P20頁例1-4)4. 閱讀教材并錄入教材例1-2,例1-5,例1-6程序,運行并輸出結果,了解#窗體應用程序的創(chuàng)建。并思考:(1)namespace關鍵字聲明命名空間有何作用?using關鍵字指明

3、引用的命名空間有何作用?三、實驗報告完成本次實驗的報告,寫清實驗目的和要求、實驗內(nèi)容和實驗結果。實驗二 基本數(shù)據(jù)類型和流程控制語句【學時數(shù):2學時】一、實驗目的1. 理解#的值類型、常量和變量的概念。2. 掌握理解數(shù)據(jù)類型的轉換以及數(shù)據(jù)類型的轉換用法;3. 掌握#的常用運算符以及表達式的運算規(guī)則。4. 掌握#的兩種分支語句(if、switch)的使用方法。5. 掌握#的種循環(huán)語句(while、do/while、for、foreach)的使用方法。6. 掌握引用類型中的字符串、數(shù)組的使用。二、實驗內(nèi)容1. 設計一個控制臺程序,從鍵盤輸入一個人的名字,在顯示器中顯示對輸入姓名的問候。比如輸入:張三

4、;輸出:張三,歡迎你!提示:輸入字符串放入s中,將s+” ,歡迎你!”,再輸出。2. 編寫一個控制臺程序,輸入一個學生的成績,輸出其等級。(優(yōu):>=90;良:>=80;中:>=70;及格:>=60;不及格:<60)。分別用if語句及switch-case語句實現(xiàn)。3. 編寫一個控制臺應用程序,求100之內(nèi)的所有“素數(shù)”。4. 打印輸出楊輝三角形。三、實驗報告完成本次實驗的報告,寫清實驗目的和要求、實驗內(nèi)容和實驗結果。四、參考程序1. /* program e2-1*/using System;namespace p21 class program static v

5、oid Main() Console.Write("please inter a person's name"); string s; s=Console.ReadLine(); Console.WriteLine("s+ 歡迎你!"); 2. using System;namespace p23 class program static void Main() Console.Write("please inter a number to x "); int x; x=int.parse(Console.ReadLine()

6、; if(x>=90) Console.WriteLine("優(yōu)秀!"); else if(x>=80) Console.WriteLine("良!"); else if(x>=70) Console.WriteLine("中!"); else if(x>=60) Console.WriteLine("及格!"); else Console.WriteLine("差!"); using System;namespace p23 class program static v

7、oid Main() Console.Write("please inter a number to x "); int x; x=int.parse(Console.ReadLine(); int i=x/10; switch(i) case 10: case 9: Console.WriteLine("優(yōu)秀!"); break; case 8: Console.WriteLine("良!"); break; case 7: Console.WriteLine("中!");break; case 6: Conso

8、le.WriteLine("及格!");break; default: Console.WriteLine("差!");break; 3. using System;namespace p23 class program public static bool prime(int m) for(ing i=2;i<m;i+) if(m%i=0) return false; return true; public static void Main() int m,k,n=1; Console.Write("0,-4",2); for

9、(m=3;m<100;m+=2) if(prime(m); Console.Write("0,-4",m); if(+n%10=0) Console.WriteLine("n"); 4.using System; class p24 public static void Main() int i,j,k,m; k=7; int Y=new int k;/定義二維鋸齒狀數(shù)組 for(i=0;i<Y.Length;i+) Yi=new inti+1; Yi0=1; Yii=1; for(i=2;i<Y.Length;i+) for(j=1;

10、j<Yi.Length-1;j+)/Yi.Length是Yi這個數(shù)組的長度 Yij=Yi-1j-1+Yi-1j; for(i=0;i<Y.Length;i+) for(j=0;j<Yi.Length-1;j+) Console.Write("0,5:d",Yij); Console.WriteLine( ); 實驗三 類和結構,接口委托與事件【學時數(shù):4學時】一、實驗目的1. 熟悉C#類、對象、方法的概念與用法;2. 掌握編寫簡單的面向對象的應用程序;3. 熟悉并掌握C#構造方法及其重載方法的用法;4. 熟悉并掌握類和成員的訪問修飾符的用法。5. 掌握抽象

11、類與接口的定義及使用方法。6. 掌握抽象類與接口的定義及使用方法。二、實驗要求1. 編寫一個Windows 窗體應用程序,實現(xiàn)以下功能。(1) 定義一個CourseInfo累,該類包含以下成員。l 具有CourseName(課程名),CourseTime(開課時間),BookName(書名),Price(定價)4個屬性,期中開課時間為枚舉類型值(秋季,春季)。l 具有一個靜態(tài)變量Counter,每創(chuàng)建一個Course實例,給變量值自動加1。l 提供無參數(shù)構造函數(shù),在構造函數(shù)中設置相應的屬性值。l 提供一個print方法,現(xiàn)實該實例的4個屬性值。(2) 在主窗體的代碼實現(xiàn)中,分別創(chuàng)建Course

12、Info實例,測試類中提供的功能,并將結果顯示出來。2. 單一繼承:用C#編寫一個程序,使用Animal和Mammal兩個類來說明一般動物和哺乳動物的繼承關系。Animal具有名稱、所屬門類等屬性,需要提供方法實現(xiàn)以接收和顯示這些屬性的值。Mammal類具有代表哺乳動物習性的屬性,這些屬性表明哺乳動物與其他類型動物的區(qū)別。同樣地,需要提供方法實現(xiàn)以接收和顯示這些屬性的值。3. 使用抽象類和抽象方法計算不同形狀的面積:編寫一個程序,使用抽象類和抽象方法來計算不同形狀的面積。4. 使用委托和事件實現(xiàn)模擬鬧鈴:編寫一個模擬鬧鈴的程序,具有鬧鈴,繼續(xù)鬧鈴,打會盹兒,停止鬧鈴的功能。(編寫一個程序用來模

13、擬鬧鐘鬧起床流程,有一個鬧鐘類AlarmClock,催促起床類WakeMeUp,使用類AlarmDriver。調(diào)用類引發(fā)開始鬧鈴事件,鬧鐘類調(diào)用委托方法進行處理,催促起床類和用戶進行交互。)三、參考程序四、參考程序1. 1. /*CourseInfo.cs using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ClassTest enum CourseTime 春季, 秋季 class CourseInfo

14、public static int Counter = 0; / <summary> / 課程名 / </summary> public string CourseName get; set; / <summary> / 開課學期 / </summary> public CourseTime CourseSemester; / <summary> / 書名 / </summary> public string BookName get; set; / <summary> / 定價 / </summary&

15、gt; public double Price get; set; public CourseInfo() Counter+; CourseName = BookName = "<null>" CourseSemester = CourseTime.春季; Price = 0; public CourseInfo(string couseName, string bookName, CourseTime courseSemester,int price) Counter+; CourseName = couseName; BookName = bookName;

16、 CourseSemester = courseSemester; Price = price; public string Print() return string.Format( "0,-101,-102,-103,5", CourseName, CourseSemester, BookName, Price); /*Mainform.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;usi

17、ng System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace ClassTest public partial class MainForm : Form public MainForm() InitializeComponent(); this.Load += MainForm_Load; void MainForm_Load(object sender, EventArgs e) listBox1.Items.Add(string.Format( &quo

18、t;0,-101,-102,-103,5", "課程名","開設學期","書名","定價"); var course1= new CourseInfo CourseName="數(shù)據(jù)結構", CourseSemester= CourseTime.春季, BookName="數(shù)據(jù)結構", Price=40 ; listBox1.Items.Add(course1.Print(); var course2 = new CourseInfo("操作系統(tǒng)"

19、, "操作系統(tǒng)", CourseTime.秋季, 45); listBox1.Items.Add(course2.Print(); var course3 = new CourseInfo("軟件工程", "軟件工程", CourseTime.春季, 38); listBox1.Items.Add(course2.Print(); /*program.csusing System;using System.Collections.Generic;using System.Linq;using System.Threading.Task

20、s;using System.Windows.Forms;namespace ClassTest static class Program / <summary> / 應用程序的主入口點。 / </summary> STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm(); 2. using System;using System.Co

21、llections.Generic;using System.Text;namespace SingleInher    /一般動物類    class Animal            protected string _name;  /動物名稱        protected string _type;  /動物所屬門類  

22、60;     /接收名稱和門類        public void AcceptDetails()                    Console.WriteLine("請輸入動物名稱:");       &#

23、160;    this._name = Console.ReadLine();            Console.WriteLine("請輸入所屬門類:");            this._type = Console.ReadLine();      

24、60;         /顯示動物的名稱和所屬門類         public void DisplayDetails()                    Console.WriteLine();     

25、60;      Console.WriteLine("0 動物的詳細信息如下:", this._name);            Console.WriteLine("姓名:0", this._name);            Console.WriteLine("門類:0

26、", this._type);                /哺乳動物類,繼承一般動物類    class Mammal : Animal            private double _temperature;   /恒定溫度值private string _isViviparous; 

27、/是否為胎生        /接收哺乳動物的特征信息        public void AcceptCharacter()                    Console.WriteLine("請輸入哺乳動物的恒定溫度:");  

28、          try                            _temperature = Convert.ToDouble(Console.ReadLine();     

29、60;                  catch /捕捉異常,例如:若輸入字母,在轉換成Decimal時會引發(fā)異常                            Con

30、sole.WriteLine("請為溫度輸入數(shù)字!");                        Console.WriteLine("該哺乳動物是否胎生:");            _isViviparous = Con

31、sole.ReadLine();                /顯示哺乳動物的特征信息        public void DisplayCharacter()                    Con

32、sole.WriteLine();            Console.WriteLine("0 的特征包括:", this._name);            Console.WriteLine("恒定溫度:0", _temperature.ToString();      

33、;      Console.WriteLine("是否胎生:0", _isViviparous);                class Program            static void Main(string args)     

34、               Mammal objM = new Mammal();            objM.AcceptDetails();/訪問父類方法            objM.AcceptCharacter(); &#

35、160;          objM.DisplayDetails();/訪問父類方法            objM.DisplayCharacter();            3. using System;abstract class Figure/抽象類定義,表示一個抽象圖形&#

36、160;   protected double x = 0, y = 0;    public Figure(double a, double b)    x = a; y = b;     public abstract void Area();  /抽象方法,無實現(xiàn)代碼,抽象圖形無法計算面積class Square : Figure            

37、;       /類Square定義,矩形類    public Square(double a, double b)        : base(a, b)        public override void Area()    /不能使用new,必須用override Console.WriteLine("矩形面積是:0", x * y);

38、      /顯示矩形面積class Circle : Figure                 /類Circle定義,圓類    public Circle(double a)        : base(a, a)        p

39、ublic override void Area()    Console.WriteLine("圓面積是:0", 3.14 * x * y);     /顯示圓的面積class Class1    static void Main(string args)            Square s = new Square(20, 30);    

40、    Circle c = new Circle(10);        s.Area();        c.Area();    4. using System;using System.Collections.Generic;using System.Text;namespace EventSample    /    

41、 / 定義事件參數(shù)類    /    public class AlarmEventArgs : EventArgs            private readonly bool snoozePressed;        private readonly int nrings;       

42、/構造函數(shù)        public AlarmEventArgs(bool snoozePressed, int nrings)                    this.snoozePressed = snoozePressed;         

43、   this.nrings = nrings;                / 定義屬性,私有字段nrings         public int NumRings                

44、0;   get return nrings;                 / 定義屬性,返回私有字段snoozePressed        public bool SnoozePressed               &

45、#160;    get return snoozePressed;                 / 下面的屬性返回一個字符串        public string AlarmText               

46、;     get                            if (snoozePressed)               

47、                     return ("別打打盹兒,快起床!");                          &

48、#160;     else                                    return ("起床!");      

49、;                                  / 聲明自定義代理類型,它的事件參數(shù)的類型為AlarmEventArgs    public delegate void AlarmEventHandler(object sender, AlarmE

50、ventArgs e);    / 包含事件和觸發(fā)事件方法的類    public class AlarmClock            private bool snoozePressed = false;        private int nrings = 0;        privat

51、e bool stop = false;        / 定義兩個屬性        public bool Stop                    get return stop;        &

52、#160;    set stop = value;                 public bool SnoozePressed                    get return snoozePressed;   

53、;          set snoozePressed = value;                 / 聲明事件,它所使用的代理類型為AlarmEventHandler         public event AlarmEventHandler Alarm;  

54、;      / 觸發(fā)事件的方法        protected virtual void OnAlarm(AlarmEventArgs e)                    if (Alarm != null)      

55、0;                     / 通過代理調(diào)用事件處理方法                Alarm(this, e);          &#

56、160;                 / 在該方法中循環(huán)調(diào)用OnAlarm方法來觸發(fā)事件,直到        / stop字段的值為true        public void Start()           

57、       for (; ; )                      nrings+;            if (stop)      &

58、#160;                       break;                          else if (s

59、noozePressed)                             / 程序暫停1秒鐘               System.Threading.Thread.Sleep

60、(1000);    / 創(chuàng)建事件參數(shù)對象實例                AlarmEventArgs e = new AlarmEventArgs(snoozePressed, nrings);             OnAlarm(e);   / 觸發(fā)事件,在事件處理方

61、法中會             / 相應地設置snoozePressed和stop              / 字段的值(通過屬性來設置)                

62、;                           else                         

63、60;     / 程序暫停0.3秒                System.Threading.Thread.Sleep(300);                AlarmEventArgs e = new AlarmEventArgs(snoozeP

64、ressed,   nrings);               OnAlarm(e);       / 觸發(fā)事件                       

65、                    / 包含事件處理方法的類    public class WakeMeUp            / 事件處理方法        / 在該方法中根據(jù)用戶的輸入來通過事件參數(shù)sender &#

66、160;      / 設置事件發(fā)送者屬性,        public void AlarmRang(object sender, AlarmEventArgs e)                Console.WriteLine(e.AlarmText + "n");    

67、;    if (!(e.SnoozePressed)                  if (e.NumRings % 10 = 0)                    Console.WriteLine("繼續(xù)

68、鬧鈴?輸入Y");           Console.WriteLine("打會盹兒?輸入N");           Console.WriteLine("停止鬧鈴?輸入Q");           String input = Console.

69、ReadLine();           if (input.Equals("Y") | input.Equals("y") return;           else if (input.Equals("N") | input.Equals("N")    

70、60;                    / 通過事件參數(shù)sender來訪問觸發(fā)事件的對象             (AlarmClock)sender).SnoozePressed = true;        &

71、#160;      return;                        else           (AlarmClock)sender).Stop = true;  

72、0;          return;                                          

73、;     else                   Console.WriteLine(" 繼續(xù)鬧鈴?輸入Y");          Console.WriteLine(" 停止鬧鈴?輸入Q");   &

74、#160;      String input = Console.ReadLine();          if (input.Equals("Y") | input.Equals("y") return;          else     &#

75、160;                    (AlarmClock)sender).Stop = true;               return;          &#

76、160;                         / 包含程序入口方法的類    public class AlarmDriver            / 入口方法        publi

77、c static void Main(string args)                    / 創(chuàng)建事件接收者對象實例            WakeMeUp w = new WakeMeUp();        

78、60;   / 創(chuàng)建事件發(fā)送者對象實例            AlarmClock clock = new AlarmClock();            / 把事件處理方法綁定到發(fā)送者的事件上            clock.Alarm +=

79、new AlarmEventHandler(w.AlarmRang);            clock.Start();            實驗四 泛型與LINQ與文件操作【學時數(shù):2學時】一、實驗目的1. 掌握泛型的使用2. 掌握path類和directory類的操作。Ø 3. 掌握文本文件二進制文件,及內(nèi)存文件的讀寫。二、實驗內(nèi)容1.輸入教材P118 例5-

80、2并運行該程序2.創(chuàng)建文件字節(jié)讀寫應用程序:先寫字節(jié)數(shù)組數(shù)據(jù)到文件,然后這一個文件所有字節(jié)到數(shù)組并在屏幕顯示的程序,然后測試移動文件讀寫位置。3. 創(chuàng)建刪除、移動、復制文件的應用程序:先判斷用戶輸入的文件路徑是否存在,如存在則進行刪除、移動、復制等操作。三、實驗報告完成本次實驗的報告,寫清實驗目的和要求、實驗內(nèi)容和實驗結果。四、參考程序1. 參見教材118頁。2. using System;using System.Collections.Generic;using System.Text;using System.IO;/使用文件必須引入的命名空間namespace FileStreamEx

81、  class Program    static void Main(string args)         WriteStream();     ReadStream();     SeekStream();     static  void WriteStream()  byte data

82、= new byte10;/建立字節(jié)數(shù)組  for (int i = 0; i < 10; i+)/為數(shù)組賦值  datai = (byte)i;  FileStream fs = new FileStream("d:/g1.bin", FileMode.Create);/建立流對象  fs.Write(data, 0, 10);/寫data字節(jié)數(shù)組中的所有數(shù)據(jù)到文件 fs.Close();/不再使用的流對象,必須關閉。垃圾收集器不能自動清除流對象   static v

83、oid ReadStream() FileStream fs = new FileStream("d:/g1.bin", FileMode.Open);/ byte data = new bytefs.Length; long n = fs.Read(data, 0, (int)fs.Length);/n為所讀字節(jié)數(shù) fs.Close();  Console.WriteLine("文件的內(nèi)容如下:");  foreach (byte m in data)  

84、;Console.Write("0,", m); static void SeekStream()   FileStream fs = new FileStream("d:/g1.bin", FileMode.Open);/無第3個參數(shù) fs.Seek(-4, SeekOrigin.End);/文件讀寫位置移到從文件尾部向前5個字節(jié) Console.WriteLine("讀寫位置:0,能定位:1", fs.Position, fs.CanSeek); Console

85、.WriteLine("能讀:0,能寫:1", fs.CanRead, fs.CanWrite); fs.Close();   3. using System;using System.Collections.Generic;using System.Text;using System.IO;namespace FileExm    class Program         static void Main(string args) 

86、             DeleteFile();        CopyFile();       MoveFile();      static void DeleteFile()      Console.WriteLine("請鍵入要刪除的文件的路徑:");    string path = Console.ReadLine();/從鍵盤讀入路徑,輸入回車結束     if (File.Exists(path)/表示其后字符串不包括轉義字符       File.Delete(path);     else    

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論