




已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
實(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來重寫方法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)自己決定,比如可以直接在控制臺輸出“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(開始播放歌曲); 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.開始播放歌曲!); Console.WriteLine(2.停止播放歌曲!); Console.WriteLine(3.下一歌曲!); Console.WriteLine(4.前一首歌曲!); CDPlayer CD=new CDPlayer(); do Console.WriteLine(請選擇:); 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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB 28374-2025電纜防火涂料
- 農(nóng)村養(yǎng)殖場廢棄物處理技術(shù)合作協(xié)議
- 如何識別和分析企業(yè)云服務(wù)提供商的性能
- 養(yǎng)殖場環(huán)保達(dá)標(biāo)合作協(xié)議
- 實(shí)驗(yàn)室安全規(guī)定
- 那場風(fēng)雨過后的景色描寫作文(15篇)
- 動物保護(hù)的重要性議論文并附加實(shí)例說明(11篇)
- 學(xué)生在職實(shí)習(xí)表現(xiàn)及成果證明(7篇)
- 2025年滑雪教練職業(yè)技能測試卷:2025年滑雪教練冰雪運(yùn)動項(xiàng)目賽事運(yùn)營與管理試題
- 2025年電子商務(wù)師(初級)職業(yè)技能鑒定試卷:電子商務(wù)平臺數(shù)據(jù)分析與客戶價(jià)值評估試題
- 運(yùn)輸公司交通安全培訓(xùn)課件
- 2025年陜西省中考數(shù)學(xué)試題(解析版)
- 北師大版7年級數(shù)學(xué)下冊期末真題專項(xiàng)練習(xí) 03 計(jì)算題(含答案)
- 小學(xué)生匯報(bào)講課件
- 職業(yè)衛(wèi)生管理制度和操作規(guī)程標(biāo)準(zhǔn)版
- 小學(xué)信息技術(shù)四年級下冊教案(全冊)
- 河道保潔船管理制度
- 2025浙江嘉興市海寧市嘉睿人力招聘5人筆試參考題庫附帶答案詳解析版
- 2025年重慶市中考物理試卷真題(含標(biāo)準(zhǔn)答案)
- 2025年安徽蚌埠市龍子湖區(qū)東方人力資源有限公司招聘筆試參考題庫含答案解析
- 2025至2030中國云計(jì)算行業(yè)產(chǎn)業(yè)運(yùn)行態(tài)勢及投資規(guī)劃深度研究報(bào)告
評論
0/150
提交評論