




已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
實驗四 類的設(shè)計和實現(xiàn)(2)實驗學(xué)時:2學(xué)時實驗類型:驗證實驗要求:必做一、實驗?zāi)康?. 掌握類的繼承的實現(xiàn);2. 掌握派生類、抽象類、抽象方法的使用;3. 了解接口的實現(xiàn);4. 了解事件的實現(xiàn)二、實驗內(nèi)容實驗1類的繼承的實現(xiàn)實驗要求:參照課本例8.3,創(chuàng)建基類Person和派生類Teacher?;怭erson包含實例字段name和age;虛函數(shù)GetInfo()顯示個人信息。派生類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(); 運行結(jié)果:實驗2 抽象類、抽象方法、多態(tài)性的實現(xiàn)實驗要求:創(chuàng)建抽象基類Shape和派生類Rectangle、Circle。利用多態(tài)性實現(xiàn)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(); 運行結(jié)果:實驗3 接口的實現(xiàn)實驗要求:聲明一個接口ICDPlayer,包含4個接口方法:Play()、Stop()、NextTrack()和PreviousTrack(),以及一個只讀屬性CurrentTrack創(chuàng)建類CDPlayer實現(xiàn)該接口,模擬CD的播放、停止、下一音軌、上一音軌的操作。(如何實現(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(); 運行結(jié)果:實驗4 事件的實現(xiàn)實驗要求:參照課本例9.7的綜合示例,實現(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(列表中增加了項目:0, args.Name); public static void Method2(object source, NameListEventArgs args) Console.WriteLine(列表中的項目數(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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 4691-2024封閉區(qū)域智能網(wǎng)聯(lián)低速養(yǎng)護車作業(yè)安全管理規(guī)范
- Unit1-單元知識圖譜課件
- 糖尿病護理五駕馬車
- 2025年科技成果轉(zhuǎn)化專項資金申請科技成果轉(zhuǎn)化模式研究報告
- 2025年家具制造業(yè)個性化定制生產(chǎn)模式下的定制家具行業(yè)市場前景預(yù)測報告
- 加重錫芯三股繩項目投資可行性研究分析報告(2024-2030版)
- DB32/T 4542-2023河網(wǎng)水功能區(qū)水環(huán)境容量核定技術(shù)規(guī)范
- 2025年數(shù)控彎管機項目投資分析及可行性報告
- DB32/T 4522-2023春蘭設(shè)施生產(chǎn)技術(shù)規(guī)程
- 酒店裝修安全責(zé)任書格式
- 家具供貨結(jié)算協(xié)議書
- 2025屆湖南省邵陽市高三下學(xué)期第三次聯(lián)考物理試卷(含答案)
- 2025年公證員資格考試全國范圍真題及答案
- 叉車作業(yè)安全協(xié)議書
- 房屋解除轉(zhuǎn)讓協(xié)議書
- 小學(xué)生美術(shù)講課課件
- 新聞采訪考試試題及答案
- JJF 2215-2025移動源排放顆粒物數(shù)量檢測儀校準規(guī)范
- 選擇性必修1 《當代國際政治與經(jīng)濟》(主觀題答題模版)
- 河北單招試題及答案英語
評論
0/150
提交評論