类别与物件IIClassesandObjectsII.ppt
《类别与物件IIClassesandObjectsII.ppt》由会员分享,可在线阅读,更多相关《类别与物件IIClassesandObjectsII.ppt(47页珍藏版)》请在三一文库上搜索。
1、1,類別與物件 II (Classes and Objects II),鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所,2,程式 DiceSimulation.Program (1/3),using System; namespace DiceSimulation /* * 模擬擲骰子示範類別的宣告與物件的使用 * 使用類別Dice的建構函式 * 3/23/2008 */ class Program static void Main(string args) ,3,程式 DiceSimulation.Program (2/3),int count = new in
2、t6; / 累計點數出現次數 for (int i = 0; i 6; +i) counti = 0; const int N = 12000; / 總擲骰次數 int seed = 123; Dice dice = new Dice( seed ); int faceValue; / 擲骰N次 for (int i = 0; i N; +i) faceValue = dice.FaceValue; +countfaceValue-1; dice.Toss(); ,4,程式 DiceSimulation.Program (3/3),/ 印出結果 for (int i = 0; i 6; +i)
3、 Console.WriteLine(“ 0 appears 1 times “, i + 1, counti); ,5,程式 DiceSimulation.Dice (1/2),using System; namespace DiceSimulation /* * 骰子 * 3/23/2008 */ public class Dice int faceValue; Random rand; public Dice() rand = new Random(); Toss(); ,6,程式 DiceSimulation.Dice (2/2),public Dice(int seed) rand
4、= new Random(seed); Toss(); public int FaceValue get return faceValue; set faceValue = value; public void Toss() faceValue = rand.Next() % 6 + 1; ,7,建構函式與解構函式 (Constructor and Destructor),預設建構函式(default constructor) 具參數之建構函式 檢驗參數範圍 解構函式,8,物件產生與消滅流程,static void main( string arg ) ,Dice dice = new Dic
5、e( seed );,public Dice(int seed) . . . ,Dice() . . . ,刪除物件dice,離開主函數, 程式結束,物件宣告,物件生成,9,存取修飾詞(Access Modifiers),public private protected internal,10,練習,修改類別Card,改用建構函式設定初值,以屬性取得suit及faceValue,Card,suit : char,Suit FaceValue,faceValue : int,11,程式 UsingStatic.Program (1/2),using System; namespace Using
6、Static /* 示範靜態成員之使用 * 3/27/2007 */ class Program static void Main(string args) Console.WriteLine( “請輸入要轉換的小時數與天數, 以一個空格分開“); string input = (Console.ReadLine().Split( ); int hours = int.Parse(input0); int days = int.Parse(input1);,12,程式 UsingStatic.Program (2/2),int hoursToMins = TimeConversion.Hour
7、sToMins(hours); int daysToHours = TimeConversion.DaysToHours(days); Console.WriteLine(hours + “ hours = “ + hoursToMins + “ minutes“); Console.WriteLine(days + “ days = “ + daysToHours + “ hours“); Test t1 = new Test(); Test t2 = new Test(); Console.WriteLine(Test.GetNConstructed + “ Test objects we
8、re constructed“); Console.ReadLine(); ,13,程式 UsingStatic.TimeConversion (1/2),using System; namespace UsingStatic /* * collection of time conversion routines * 3/27/2007 */ public static class TimeConversion private const int HOURS_PER_DAY = 24; private const int MINS_PER_HOUR = 60;,14,程式 UsingStati
9、c.TimeConversion (2/2),public static int HoursToMins( int hours ) return hours*MINS_PER_HOUR; public static int DaysToHours( int days ) return days*HOURS_PER_DAY; ,15,程式 UsingStatic.Test,using System; namespace UsingStatic public class Test private static int nConstructed = 0; public Test() +nConstr
10、ucted; public static int GetNConstructed get return nConstructed; ,16,靜態成員與靜態類別,常數宣告 靜態成員應用場合 靜態函式Main 靜態類別應用場合,17,靜態成員的記憶配置,t1,記憶體地址1,t2,記憶體地址2,函式成員Test進入點,函式Test()進入地址,函式Test()進入地址,nConstructed,Test,函式成員 GetNConstructed進入點,記憶體地址,18,練習 (1/2),在類別Card內增加靜態成員函式,累計產生的Card物件數 寫一程式利用類別Card產生全副撲克牌,放在陣列dec
11、k內,19,練習 (2/2),實作並測試一靜態類別EqSolver,內含兩靜態成員函式double Linear(double a, double b)及double Quadratic(double a, double b, double c)分別解一次方程式a x + b = 0及a x2 + b x + c = 0,20,運算子多載,可以多載 一元: +、-、!、+、-、true、false 二元:+、-、*、/、%、&、|、=、!=、=、new、is、sizeof、typeof、+=、-=、*=、/=、%=、&=、|=、=、= 轉換運算子 explicit 關鍵字 Implicit 關
12、鍵字,21,UsingOperOverload.Program(1/2),using System; namespace UsingOperOverload /* 示範一元運算子多載 * 4/9/2007 */ class Program static void Main(string args) Rectangle rec = new Rectangle(100, 50); Console.WriteLine(“長方形rec的面積: “ + rec.Area();,22,UsingOperOverload.Program(2/2),rec+; Console.WriteLine(“長方形re
13、c+後的面積: “ + rec.Area(); / 應為 5151 +rec; Console.WriteLine(“長方形+rec後的面積: “ + rec.Area(); / 應為 5304 Console.ReadLine(); ,23,UsingOperOverload.Rectangle(1/2),using System; namespace UsingOperOverload public class Rectangle private int width; private int length; private int area; public Rectangle(int wi
14、dth, int length) this.width = width; this.length = length; area = width * length; ,24,UsingOperOverload.Rectangle(2/2),public int Area() return area; public static Rectangle operator +(Rectangle op) Rectangle result = new Rectangle(op.width+1, op.length+1); return result; ,25,UsingOperOverload2.Prog
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 类别 物件 IIClassesandObjectsII
链接地址:https://www.31doc.com/p-2598001.html