物件与类别.ppt
《物件与类别.ppt》由会员分享,可在线阅读,更多相关《物件与类别.ppt(119页珍藏版)》请在三一办公上搜索。
1、1,物件與類別,鄭士康國立台灣大學電機工程學系/電信工程研究所/資訊網路與多媒體研究所,2,綱要,由函式到類別 屬性 函式成員之多載建構式與解構式this 參考靜態成員結構複製建構式物件作為函式參數物件陣列,3,綱要,由函式到類別 屬性 函式成員之多載建構式與解構式this 參考靜態成員結構複製建構式物件作為函式參數物件陣列,4,三個矩形物件,a,b,c,3,5,2,2,4,1,5,旋轉90o,a,b,c,3,5,2,2,4,1,6,RotatingRectangles0.Program.Main片段,/矩形aint width_a=3;int height_a=5;/矩形bint width
2、_b=2;int height_b=2;/矩形cint width_c=4;int height_c=1;/旋轉90度Rotate(ref width_a,ref height_a);Rotate(ref width_b,ref height_b);Rotate(ref width_c,ref height_c);,7,RotatingRectangles0.Program.Rotate,static void Rotate(ref int width,ref int height)int temp=width;width=height;height=temp;,8,討論,矩形a的寬與高,分開
3、宣告為width_a與height_a靠註解才能知道它們屬於同一個矩形 旋轉90o應該是矩形物件具有的功能,在程式中卻完全看不出來單看程式碼很難體會它代表將矩形旋轉90o 使用幾乎沒有限制即使兩個參數並不代表某個矩形物件的寬與高,任何程式師還是可以透過呼叫函式Rotate,來交換任兩個整數變數的數值,9,矩形物件列表,10,旋轉90o後矩形物件列表,11,矩形類別,12,C#Class Rectangle,class Rectangle public int width;public int height;public void Rotate()int temp=width;width=hei
4、ght;height=temp;,13,物件、類別,物件(Object)狀態與功能:例如,矩形a,電視機甲類別(Class)藍圖:例如,矩形類別,泛稱電視機狀態:成員變數(member variables)如width,height功能:成員函式(member function)如Rotate,14,類別Rectangle之成員函式Rotate,功能為交換封裝一起的成員變數width和height之值不需要由外界輸入參數 不用加上關鍵字static,15,RotatingRectangles.Program.Main片段,Rectangle a=new Rectangle();a.width=
5、3;a.height=5;Rectangle b=new Rectangle();b.width=2;b.height=2;Rectangle c=new Rectangle();c.width=4;c.height=1;a.Rotate();b.Rotate();c.Rotate();,16,新增類別程式,專案加入類別類別輸入類別名稱.cs,17,RotatingRectangles.Rectangle框架程式碼,using System;using System.Collections.Generic;using System.Linq;using System.Text;namespac
6、e RotatingRectangles class Rectangle,18,封裝(Encapsulation),類別將狀態變數與功能函式封裝起來使類別內容的修改更容易若能把類別變成黑箱(black box)模組,使用的程式師不用了解成員變數與成員函式的細節,就能應用類別的功能程式碼重複使用(code reuse)函式導向的程式也可將函式當作黑箱模組相關的變數沒有結合在一起程式碼重複使用不是那麼方便,19,堆疊(Stack)與堆積(Heap),Stack,.,20,實值型別儲存方式,堆疊(Stack),int x=100;,100,x,21,參考型別儲存方式,堆疊(Stack),string
7、 x=“abc”;,x,參考,a,b,c,堆積(Heap),22,矩形物件記憶配置,23,成員函式,同類別的物件,同一個成員函式只有一套函式程式碼各物件就只存放這個程式碼進入點的位置 函式程式碼通常佔據相當大的記憶,如果每個物件都存放一套函式程式碼,需要很多記憶體不同物件呼叫自己的成員函式,使用物件自己的成員變數會把物件在堆積記憶區的地址(稱為this)也傳給成員函式 由this成員函式可知道如何取用對應的成員變數,24,private與public,一般電視機可以讓一般使用者調整的按鈕通常顯露在外不讓使用者碰觸的電路裝置,都封裝在機殼內,對使用者來說無異黑箱 類別相當於電視內部電路,作為自身
8、工具幫手的成員變數與成員函式,宣告時加上關鍵字private,不讓外界程式師取用像是電視的操控按鈕,提供給外界使用的成員變數與成員函式,則在宣告時加上關鍵字public 視為進一步的封裝,25,軟體IC,硬體IC內部的電路封裝在內,不輕易顯露電子電路工程師應用IC時,只要在接腳施加正確的電壓電流訊號,IC便能正常工作,大大減輕電子電路工程師的負擔類別的封裝軟體工程師不需要了解private成員變數與成員函式只要懂得public成員的使用,也能正確應用既有類別程式,達到減輕工作負荷,增進效率的目的,26,封裝,通常狀態變數為private,只供同類別之功能函式取用,不可以由類別外直接取用若有必要
9、讓外界取得或設定其值,通常另外再寫public的成員函式來處理避開同名變數問題避免被不慎改動,破壞狀態的正確一致性容易維護,不影響外界程式功能函式可能公用(public),也可能私有一類別至少有一公用函式,以便應用,27,RotatingRectangles2.Rectangle片段(1/2),private int width;private int height;public void SetWidth(int w)width=w;public int GetWidth()return width;public void SetHeight(int h)height=h;,28,Rotat
10、ingRectangles2.Rectangle片段(2/2),public int GetHeight()return height;public void Rotate()int temp=width;width=height;height=temp;,29,RotatingRectangles2.Program.Main片段(1/2),Rectangle a=new Rectangle();a.SetWidth(3);a.SetHeight(5);Rectangle b=new Rectangle();b.SetWidth(2);b.SetHeight(2);Rectangle c=ne
11、w Rectangle();c.SetWidth(4);c.SetHeight(1);a.Rotate();b.Rotate();c.Rotate();,30,RotatingRectangles2.Program.Main片段(2/2),Debug.Assert(a.GetWidth()=5,31,偵錯器追踪,物件的成員變數值逐步執行或按F11鍵,至進入類別目前類別名稱、函式名稱函式區域變數及參數this及物件的成員變數值,32,綱要,由函式到類別 屬性 函式成員之多載建構式與解構式this 參考靜態成員結構複製建構式物件作為函式參數物件陣列,33,設定或取得成員變數的值,通常將成員變數宣告
12、為private,另外宣告public的成員函式來設定或取得成員變數的值,以維護成員變數間的一致性 類別中增加了很多只是要Set和Get成員變數值的簡單函式,使類別宣告有一點冗長,而呼叫這些函式時也得加上括弧,有一點累贅,34,屬性(Attribute),兼顧成員變數間的一致與程式寫法的簡潔 成員函式SetWidth和GetWidth可以整合為一個屬性Width,成員函式SetHeight和GetHeight可以整合為一個屬性Height將屬性名稱取成與成員變數相同 唯一差別為開始字母之大小寫屬性名稱也可以與成員變數不同此處的作法可以省去找新名稱的麻煩,並使屬性與成員變數的關連清楚,35,Ro
13、tatingRectangles3.Program.Main片段(1/2),Rectangle a=new Rectangle();a.Width=3;a.Height=5;Rectangle b=new Rectangle();b.Width=2;b.Height=2;Rectangle c=new Rectangle();c.Width=4;c.Height=1;a.Rotate();b.Rotate();c.Rotate();,36,RotatingRectangles3.Program.Main片段(2/2),Debug.Assert(a.Width=5,37,RotatingRect
14、angles3.Rectangle片段,private int width;private int height;public int Width set width=value;get return width;public int Height set height=value;get return height;,38,DiceSimulation.Program.Main片段,int count=0,0,0,0,0,0;int N=12000;Dice dice=new Dice();for(int i=0;i N;+i)dice.Toss();countdice.FaceValue-
15、1+;for(int k=0;k 6;+k)Console.WriteLine(0 appears 1 times,k+1,countk);,39,DiceSimulation.Dice,class Dice private int faceValue=1;private Random rand=new Random();public int FaceValue get return faceValue;public void Toss()faceValue=rand.Next()%6+1;,40,投擲兩顆骰子,Dice dice1=new Dice();Dice dice2=new Dice
16、();dice1.Toss();dice2.Toss();Console.WriteLine(dice1.FaceValue);Console.WriteLine(dice2.FaceValue);非常可能發現兩顆骰子擲出的結果相同dice1和dice2幾乎同時建立成員變數rand建立時,會使用相同的計算機時間做為亂數產生器的種子,使兩者得到的亂數數列完全相同,41,綱要,由函式到類別 屬性 函式成員之多載建構式與解構式this 參考靜態成員結構複製建構式物件作為函式參數物件陣列,42,函式多載(Overloading),變數、函式、物件、類別都要選擇適切的名稱,程式才易寫易讀適當的名稱常常不
17、夠應用FORTRAN語言函式庫的例子讓計算實數參數與複數參數正弦函數的函式,雖然演算法不同,都命名為SIN計算機自動依照參數型別的不同來決定該呼叫那一個函式 C+、Java、C#採相似的做法,43,分辨類別的成員函式,主要靠函式名稱,其次是參數的個數與型別 名稱相同,但是參數的個數或型別有所不同時,就可認為是不同的函式 例:模擬時鐘運作時間的呈現可以是二十四小時制,也可以是區分上下午(AM、PM)的十二小時制如果設定時間(SetTime)與取得時間(GetTime)的函式,對兩種不同時間呈現方式要取不同名稱,並不太容易,即使能夠,也會使函式名稱變得很長,不易使用,44,ClockSimulat
18、ion.Program.Main片段,Clock c=new Clock();c.SetTime(22,40,23);c.Tick();c.GetTime(out h,out m,out s);Debug.Assert(h=22,45,ClockSimulation.Program.Clock片段(1/4),private int hour=0;/023private int minute=0;/059private int second=0;/059/24小時制public void SetTime(int h,int m,int s)SetValues(h,m,s);/上下午制public
19、 void SetTime(int h,int m,int s,string aAMPM)SetValues(h,m,s);if(aAMPM=PM,46,ClockSimulation.Program.Clock片段(2/4),private void SetValues(int h,int m,int s)hour=h;minute=m;second=s;/24小時制public void GetTime(out int h,out int m,out int s)h=hour;m=minute;s=second;,47,ClockSimulation.Program.Clock片段(3/4
20、),/上下午制public void GetTime(out int h,out int m,out int s,out string aAMPM)h=hour;if(hour 12)h-=12;m=minute;s=second;if(hour=12)aAMPM=PM;else aAMPM=AM;,48,ClockSimulation.Program.Clock片段(4/4),/前進一秒public void Tick()second+;if(second=60)second=0;minute+;if(minute=60)minute=0;hour+;if(hour=24)hour=0;,4
21、9,綱要,由函式到類別 屬性 函式成員之多載建構式與解構式this 參考靜態成員結構複製建構式物件作為函式參數物件陣列,50,初始值設定,建立物件後,必需以屬性或成員函式指定成員變數的值如果忘了,就發生錯誤沒有設定成員變數初值的錯誤,通常稱為初始值未設定錯誤(initialization error)最好在建立物件時便能設定必要成員變數的初值,51,RotatingRectangles4.Program.Main片段,Rectangle a=new Rectangle(3,5);Rectangle b=new Rectangle(2);Rectangle c=new Rectangle(4,1
22、);a.Rotate();b.Rotate();c.Rotate();Debug.Assert(a.Width=5,52,RotatingRectangles4.Rectangle片段(1/2),private int width;private int height;public Rectangle()width=1;height=1;public Rectangle(int w,int h)width=w;height=h;public Rectangle(int w)width=w;height=w;,53,RotatingRectangles4.Rectangle片段(2/2),pub
23、lic int Width get return width;public int Height get return height;public void Rotate()int temp=width;width=height;height=temp;,54,DiceSimulation2.Program.Main 片段,int count=0,0,0,0,0,0,0,0,0,0,0;int N=12000;const int SEED_1=168;Dice dice1=new Dice(SEED_1);const int SEED_2=777;Dice dice2=new Dice(SEE
24、D_2);for(int i=0;i N;+i)dice1.Toss();dice2.Toss();count dice1.FaceValue+dice2.FaceValue-2+;,55,DiceSimulation2.Dice片段,private int faceValue=1;private Random rand;public Dice()rand=new Random();public Dice(int seed)rand=new Random(seed);public int FaceValue get return faceValue;Dice(),56,解構式(Destruct
25、or),Dice()與建構式相對,用於物件將被消滅的時候 會將物件佔用的堆積記憶區使用權歸還給系統,以便系統可以把這些記憶體分配給其他物件使用稱為停用記憶區收集(garbage collection)把不再使用的資源釋出例如,某個處理檔案的物件,在建構式中開啟檔案,開始應用,當物件要被消滅時,便需在解構式中關閉檔案,以便他處可以再使用同一檔案,57,記憶漏失(Memory Leakage),記憶區回收的工作可能相當複雜,處理不好,就容易有忘記回收的區域忘記回收的區域逐漸累積,就可能使程式繼續執行所需的記憶體不足,稱為記憶漏失先前的程式語言如C+,經驗不足的程式設計師很容易就寫出有記憶漏失的程式
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 物件 类别

链接地址:https://www.31ppt.com/p-5598514.html