




已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
實驗四 類的設計和實現(2)實驗學時:2學時實驗類型:驗證實驗要求:必做一、實驗目的1. 掌握類的繼承的實現;2. 掌握派生類、抽象類、抽象方法的使用;3. 了解接口的實現;4. 了解事件的實現二、實驗內容實驗1類的繼承的實現實驗要求:參照課本例8.3,創(chuàng)建基類Person和派生類Teacher?;怭erson包含實例字段name和age;虛函數GetInfo()顯示個人信息。派生類Teacher除了包含基類的name和age字段,還包含自己的TeacherID字段,并使用關鍵字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(); 運行結果:實驗2 抽象類、抽象方法、多態(tài)性的實現實驗要求:創(chuàng)建抽象基類Shape和派生類Rectangle、Circle。利用多態(tài)性實現Area(計算面積)和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(); 運行結果:實驗3 接口的實現實驗要求:聲明一個接口ICDPlayer,包含4個接口方法:Play()、Stop()、NextTrack()和PreviousTrack(),以及一個只讀屬性CurrentTrack創(chuàng)建類CDPlayer實現該接口,模擬CD的播放、停止、下一音軌、上一音軌的操作。(如何實現自己決定,比如可以直接在控制臺輸出“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(); 運行結果:實驗4 事件的實現實驗要求:參照課本例9.7的綜合示例,實現事件處理。源程序: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(列表中增加了項目:0, args.Name); public static void Method2(object source, NameListEventArgs args) Console.WriteLine(列表中的項目數: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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)療機構醫(yī)護人員職業(yè)操守履行及醫(yī)療安全責任合同
- 海外務工人員直付型醫(yī)療保險合作協(xié)議
- 寵物醫(yī)院寵物醫(yī)療科研合作與加盟合同
- 活動臨時隔離欄租賃與活動場地臨時衛(wèi)生服務合同
- 網絡文學平臺內容審核與版權保護合作協(xié)議
- 網紅特色燒烤店品牌授權加盟與區(qū)域市場合作協(xié)議
- 網絡交易平臺網店信息管理及合同執(zhí)行監(jiān)督協(xié)議
- 企業(yè)標準補充協(xié)議(知識產權保護)
- 城市污水處理設施運行維護補充協(xié)議
- 子女勞動教育實踐基地與學校合作框架協(xié)議
- 2024火電機組金屬監(jiān)督工作任務表
- 抗凝藥術前停藥指南
- 中國血脂管理指南(基層版2024年)
- JTS-167-2-2009重力式碼頭設計與施工規(guī)范
- 阿替普酶的藥理作用及應用
- 2024年學生團干部技能大賽考試題庫350題(含答案)
- 走進歌劇世界智慧樹知到期末考試答案章節(jié)答案2024年北京航空航天大學
- (正式版)YST 1682-2024 鎂冶煉行業(yè)綠色工廠評價要求
- 口腔實習生培訓
- 家庭教育指導流程
- 生產性服務業(yè)集聚對我國制造業(yè)全球價值鏈地位影響的門檻效應研究
評論
0/150
提交評論