1 / 51

軟體開發概論 第二 章、物件導向設計 簡介

軟體開發概論 第二 章、物件導向設計 簡介. Introduction to Object-Oriented Programming. 目標領域矩陣 Objective Domain Matrix. 物件 Objects. 物件 導向程式設計 以物件為中心的程式設計方式 自主的個體,不須外求 遇到事情,自己知道該 怎麼辦 物件是一種資料結構 屬性( Property ) 資料,代表狀態 方法( Method ) 副程式,代表行為動作. 類別 Classes. 定義物件 藍圖、設計圖 An object is an instance of a class .

flo
Download Presentation

軟體開發概論 第二 章、物件導向設計 簡介

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 軟體開發概論第二章、物件導向設計簡介 Introduction to Object-Oriented Programming

  2. 目標領域矩陣Objective Domain Matrix

  3. 物件Objects • 物件導向程式設計 • 以物件為中心的程式設計方式 • 自主的個體,不須外求 • 遇到事情,自己知道該怎麼辦 • 物件是一種資料結構 • 屬性(Property) • 資料,代表狀態 • 方法(Method) • 副程式,代表行為動作

  4. 類別Classes • 定義物件 • 藍圖、設計圖 • An object is an instance of a class. • An instance of:一份

  5. 定義 C# 類別

  6. 方法Methods • 方法相當於『副程式』或『函式』 • 副程式 Subroutine • 函式 Function • 由存取層級、傳回型別、名稱、參數和程式區塊構成

  7. 建構子Constructors 和類別同名的方法 物件初始化 快捷:輸入 ctor按 Tab

  8. 建立物件 關鍵字:new

  9. 屬性Properties 關鍵字:get、set、value

  10. 參考自己 this

  11. 方法的『簽章』Signature • Signature 是 Method 的識別依據,Signature 相同的就是同一個 Method,Signature 不同的就是不同的 Method • Method 的 Signature 涵蓋 • Method Name 方法的名稱(區分英文字母大小) • 參數的型態、型別、排列順序(由左至右) • Method 的 Signature 不涵蓋傳回值型別和 params

  12. A set of overloaded method declarations along with their signatures.

  13. Delegates(會議代表) • Delegate 是一種可以把『方法』當成參考對象的機制 • 相當於幫某一種特定長相(Signature)的『方法』取一個名字 • 此處的 RectangleHandler參考到任何『無傳回值而且接受一個Rectangle物件作為參數』的方法 • DisplayArea的 Signature 符合RectangleHandler的要求

  14. Delegate不使用 delegate,直接呼叫

  15. Delegate透過 delegate 呼叫方法 • 宣告方式:delegate result-type identifier ([parameters]); • 流程: • 宣告(Declaration) • 建立物件(Instantiation) • 調用(Invocation)

  16. Delegate透過 delegate 呼叫方法

  17. Delegate透過 delegate 呼叫 static function

  18. Delegate透過 delegate 呼叫 static function

  19. Delegate透過 delegate 呼叫 member function

  20. Delegate透過 delegate 呼叫 member function

  21. Delegate透過 delegate 呼叫 member function

  22. Delegate透過 delegate 進行 multicast

  23. Delegate透過 delegate 進行 multicast

  24. Delegate透過 delegate 進行 multicast

  25. Delegate透過 delegate 進行 multicast Console File

  26. Events(事件) 當特定事件發生時,通知其他 出版者(Publisher)=> 訂閱者(Subscriber)

  27. 訂閱事件Subscribing to Events 事件處理常式的 Signature 必須符合事件的 delegate

  28. 命名空間Namespaces • 避免命名衝突 • 將性質相近的類別集合在一起 • System:基礎類別 • System.Data:資料存取用的類別 • System.Web:Web 相關的類別 • System.Text:文字處理相關的類別

  29. 命名空間Namespaces

  30. 靜態成員Static Members • 關鍵字:static • 只有一份 • Single instance • 透過類別名稱存取 • Math.Sqrt() • Math.PI • DateTime.Now

  31. 靜態成員Static Members

  32. 資料值 & 參考Values and References 資料值在記憶體內佔用空間儲存資料 參考(Reference)儲存的不是資料,而是另一個物件在記憶體中的位置

  33. 資料值 & 參考Values and References

  34. 資料值 & 參考Values and References

  35. 封裝 Encapsulation 限制存取 隱藏內部結構

  36. 繼承 Inheritance • 繼承 • 基底類別 Base Class • 衍生類別 Derived Class • struct不支援繼承

  37. 繼承 Inheritance

  38. 抽象類別Abstract Classes • 作為共同的基底類別 • 缺乏完整的實作 • 可以用『抽象類別』當作介面存取其衍生類別建立的物件 • 不可以 new 抽象類別 • 必須繼承之後,實作所有的抽象方法 • Abstract method 只有『簽章』沒有實作

  39. 抽象類別Abstract Classes

  40. 密封類別Sealed Classes 禁止繼承

  41. 繼承自 Object .NET Framework 裡頭所有的類別都是 Object 的衍生類別

  42. 型別轉換 Casting 衍生類別物件可以轉換成基底類別物件 反過來做,會發生 System.InvalidCastException

  43. is 運算子 為了避免 InvalidCastException,先以 is 判斷型別轉換是否相容

  44. as 運算子 • 安全的型別轉換 • 轉換失敗,傳回 null

  45. 多型 Polymorphism 衍生類別共用基底類別的特性,又擁有自我獨特的性質

  46. 多型 Polymorphism

  47. 多型 Polymorphism

  48. override、new、virtual • override 以衍生類別的方法覆蓋基底類別的方法(簽章必須相同) • 改變行為 • virtual 以衍生類別的實作為準 • new 在衍生類別建立新的實作,隱藏基底類別的實作

  49. 介面 Interfaces 建立一致的存取方式 讓不同的類別也可以用相同的方式存取

More Related