實(shí)驗(yàn)五 C#類的設(shè)計(jì)和實(shí)現(xiàn),(2).doc_第1頁(yè)
實(shí)驗(yàn)五 C#類的設(shè)計(jì)和實(shí)現(xiàn),(2).doc_第2頁(yè)
實(shí)驗(yàn)五 C#類的設(shè)計(jì)和實(shí)現(xiàn),(2).doc_第3頁(yè)
實(shí)驗(yàn)五 C#類的設(shè)計(jì)和實(shí)現(xiàn),(2).doc_第4頁(yè)
實(shí)驗(yàn)五 C#類的設(shè)計(jì)和實(shí)現(xiàn),(2).doc_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

實(shí)驗(yàn)四 類的設(shè)計(jì)和實(shí)現(xiàn)(2)實(shí)驗(yàn)學(xué)時(shí):2學(xué)時(shí)實(shí)驗(yàn)類型:驗(yàn)證實(shí)驗(yàn)要求:必做一、實(shí)驗(yàn)?zāi)康?. 掌握類的繼承的實(shí)現(xiàn);2. 掌握派生類、抽象類、抽象方法的使用;3. 了解接口的實(shí)現(xiàn);4. 了解事件的實(shí)現(xiàn)二、實(shí)驗(yàn)內(nèi)容實(shí)驗(yàn)1類的繼承的實(shí)現(xiàn)實(shí)驗(yàn)要求:參照課本例8.3,創(chuàng)建基類Person和派生類Teacher?;怭erson包含實(shí)例字段name和age;虛函數(shù)GetInfo()顯示個(gè)人信息。派生類Teacher除了包含基類的name和age字段,還包含自己的TeacherID字段,并使用關(guān)鍵字override來(lái)重寫方法GetInfo()。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication12 public class Person public string name; public uint age; public Person(string name, uint age) = name; this.age = age; public virtual void GetInfo() Console.WriteLine(Name:0, name); Console.WriteLine(Age:0, age); public class Teacher : Person public string TeacherID; public Teacher(string name, uint age, string id) : base(name, age) this.TeacherID = id; public override void GetInfo() base.GetInfo(); Console.WriteLine(TeacherID:0, TeacherID); class Program static void Main(string args) Teacher objteacher = new Teacher(Zhangying, 20, 1245713131); objteacher.GetInfo(); Console.ReadKey(); 運(yùn)行結(jié)果:實(shí)驗(yàn)2 抽象類、抽象方法、多態(tài)性的實(shí)現(xiàn)實(shí)驗(yàn)要求:創(chuàng)建抽象基類Shape和派生類Rectangle、Circle。利用多態(tài)性實(shí)現(xiàn)Area(計(jì)算面積)和Show(顯示圖形名稱和面積)抽象方法。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication13 public abstract class Shape private string m_id; public Shape(string s) Id= s; public string Id get return m_id; set m_id = value; public abstract double Area get; public override string ToString() return Id + Area= + string.Format(0:F2, Area); public class Rectangle : Shape private int m_width; private int m_height; public Rectangle(int width, int height, string id) : base(id) m_width = width; m_height = height; public override double Area get return m_height * m_width; public class Circle : Shape private int m_radius; public Circle(int radius, string id) : base(id) m_radius = radius; public override double Area get return m_radius * m_radius * System.Math.PI; class Program static void Main(string args) Shape shapes= new Circle (3,Circle # 1), new Rectangle(4,5,Rectangle # 1) ; Console.WriteLine(Shape Colection); foreach (Shape s in shapes) Console.WriteLine(s); Console.ReadKey(); 運(yùn)行結(jié)果:實(shí)驗(yàn)3 接口的實(shí)現(xiàn)實(shí)驗(yàn)要求:聲明一個(gè)接口ICDPlayer,包含4個(gè)接口方法:Play()、Stop()、NextTrack()和PreviousTrack(),以及一個(gè)只讀屬性CurrentTrack創(chuàng)建類CDPlayer實(shí)現(xiàn)該接口,模擬CD的播放、停止、下一音軌、上一音軌的操作。(如何實(shí)現(xiàn)自己決定,比如可以直接在控制臺(tái)輸出“CD播放中”等等)。源程序:using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication15 public interface ICDPlayer void Play(); void Stop(); void NextTrack(); void PreviousTrack(); int CurrentTrack get; public class CDPlayer:ICDPlayer private int currentTrack = 0; public int CurrentTrack get return currentTrack; public void Play() Console.WriteLine(開(kāi)始播放歌曲); Console.WriteLine(CD.CurrentTrack=0,currentTrack); public void Stop() Console.WriteLine (停止播放歌曲) ; public void NextTrack() Console.WriteLine(下一歌曲); currentTrack+; Console.WriteLine(CD.CurrentTrack=0,currentTrack); public void PreviousTrack() Console.WriteLine(前一首歌曲); if (currentTrack = 1) currentTrack-; Console.WriteLine(CD.CurrentTrack=0, currentTrack); static void Main(string args) string selection; Console.WriteLine(該程序控制歌曲播放!); Console.WriteLine(1.開(kāi)始播放歌曲!); Console.WriteLine(2.停止播放歌曲!); Console.WriteLine(3.下一歌曲!); Console.WriteLine(4.前一首歌曲!); CDPlayer CD=new CDPlayer(); do Console.WriteLine(請(qǐng)選擇:); selection = Console.ReadLine(); switch (selection) case 1: CD.Play(); break; case 2: CD.Stop(); break; case 3: CD.NextTrack(); break; case 4: CD.PreviousTrack(); break; while (selection !=2); Console.ReadKey(); 運(yùn)行結(jié)果:實(shí)驗(yàn)4 事件的實(shí)現(xiàn)實(shí)驗(yàn)要求:參照課本例9.7的綜合示例,實(shí)現(xiàn)事件處理。源程序:using System;using System.Collections;using System.Text;namespace ConsoleApplication14 public class NameListEventArgs : EventArgs private string name; public string Name get return ; set = value; private int count; public int Count get return this.count; set this.count = value; public NameListEventArgs(string name, int count) Name = name; Count = count; public delegate void NameListEventHandler(object source, NameListEventArgs args); public class NameList ArrayList list; public event NameListEventHandler nameListEvent; public NameList() list = new ArrayList(); public void Add(string Name) list.Add(Name); if (nameListEvent != null) nameListEvent(this, new NameListEventArgs(Name, list.Count); public class EventDemo public static void Method1(object source, NameListEventArgs args) Console.WriteLine(列表中增加了項(xiàng)目:0, args.Name); public static void Method2(object source, NameListEventArgs args) Console.WriteLine(列表中的項(xiàng)目數(shù):0, args.Count); public static void Main() NameList n1 = new NameList(); ListEvent += new NameListEventHandler(EventDemo.Method1); ListEvent += new NameListEventHandler(EventDemo.Method2); n1.Add(張三

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論