Saturday, May 15, 2010

OOP 四大觀念

1. 抽象(Abstract)
2. 封裝(Encapsulation)
3. 繼承(Inheritance)
4. 多型(Polymorphism)

1.抽象
抽象是定義物件基本原則屬性範圍,使外顯物件有其準則
An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewr.
抽象的好處:易於查找物件部份細節內容,可彈性運用

2.封裝
封裝又稱黑盒子(black box),意指你不需知其物件背後運作方法,只要知如何使用即可
隱含無關之物件間不應相互使用呼叫

3.繼承
使其子類得到父類之所有方法、屬性
可擴充物件功能

使用接口(interface)或抽象類(abstract class)的異點
a.抽象類可帶實作之方法
b.接口內新增方法,會破壞其子類(所以最好新增接口)
c.子類接口可很多,但其繼承只會有一類


4. 多型

//=========================//
子類最好繼承接口,而不是直接實作之方法
(Program to an interface, not an implementation)
a. 保有彈性及重覆使用性
b. 管理依存度

抽象類或接口都可當子類實體化之種類
Programming to the interface but instantiates through the subclass
ex: var subclass:AbstractClass=new SubClass();

No comments:

Post a Comment