# OOP Def: 一種軟體的設計方式,其概念為 object * what is object ? * 廣義: OOP 裡面所有東西都是 object * 狹義: Data + Behavior = Object (ie Object is a virtual entity with specific list of properties which can distinguish it from other objects and behavior which allow to manipulate with these properties) * class 的實例即為 object * what is a class? * class is a template for objects OOP 優點:modularity, scalability, lower cost of development, security & reliability ## OOP Principles ### inheritance 繼承 Inheritance allows you to create new class based on another one. New class can extend existing one and share properties and behavior. 好處: 避免重複造輪 名詞解釋: New class 又叫做 child class, basic class 又叫做 parent class ## Encapsulation 封裝 keep data and code that can manipulate this data together. ### 修飾符 * Default: * Protected * Public: 可被任意其他類別存取 * Private: 只能在該類別內部存取,不能被其他類別存取 ## Polymorphism One interface and multiple implementations. (method overwriting) ## Abstraction 對特定案例若抽象的好,就可以避免重複的code ---- ## 1-1 Classes and Object class 組成包含: constructor, fields, methods, initialization blocks, nested classes ### Class 的 Property: Field 儲存 class 的狀態或是屬性。在 class 中宣告 Field 可以有不同的 modifier ## initialization blocks 分為 static and non static 使用時機:有些 fields 想在初始化帶上預設值 static block initialization 範例: driver class