9

什麼是物件導向(3):Polymorphism

 2 years ago
source link: http://teddy-chen-tw.blogspot.com/2012/01/3polymorphism.html?showComment=1632974702196
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

什麼是物件導向(3):Polymorphism

January 26 23:01~January 27 00:23

AM-JKLVfB17CVigJFPT3qFCY1Sa0bLRxE68jyl_voevJS0Dyzxr9slNcHogYVZuAC4EMBZuKYbTBRG8-qEULKxOhnThMM8JsSMzeYCIyg7oKfMkapkyOgqlmg1zkcy4elcip64XVkTExtTBIDgHDe7KW13xhpg=w1631-h2003-no?authuser=0

Polymorphism,中文翻譯成「多型」,說真的這個概念當年Teddy學OO的時候搞了好久才弄懂,後來看了「課本」的定義之後,覺得寫得還滿好的,請參考Object-Oriented Software Engineering: A  Use Case Driven Approach:

Polymorphism means that the sender of a stimulus does not need to know the receiving instance’s class. The receiving instance can belong to an arbitrary class.

再看一下55頁中另一段類似的解釋

If an instance sends a stimulus to another instance, but does not have to be aware of which class the receiving instance belongs to, we say that we have polymorphism.

翻成白話文就是說:一個訊息(message or event or stimulus)的意義是由接收者(接收到這個訊息的物件)來解釋,而不是由訊息發出者(sender)來解釋。所以,在runtime時只要接受者換成不同的物件或是instance,系統的行為就會改變。具有這樣的特性就稱之為polymorphism。還是不懂,對,很正常。不過在看一個活生生的例子鄉民們就應該懂了。

Teddy(sender,等一下準備送出信息的物件)走在路上看到前方有兩位名人,分別是「林志玲」與「阿美姐」(等一下準備接收訊息的兩個物件),於是大喊一聲「美女請留步(訊息)」。理論上Teddy期待只有「林志玲」會「回頭」(訊息接收者的行為),沒想到「阿美姐」也回頭了...XD。所以說,一個訊息的解釋是由接收者來決定的,而不是送出者。如果一個系統具有這樣的特性,那麼我們就說這個系統具備多型的行為。

舉個程式的例子:

List<String> list = new LinkedList<String>();

list.add(“This is a book”);

從上面這一行程式來看,我們可以說寫程式的人送了一個add這個訊息給list這個instance。從靜態程式碼來看,鄉民們一定知道list.add(“This is a book”)的行為,但是如果list這個instance不是像上面程式一樣寫死new LinkedList<String>();,而是runtime時傳入的任意符合List介面的某個instance,那麼程式的行為就無法只從靜態的程式碼得知了,而且add(“This is a book”);訊息的解釋也只能看runtime時list instance指到哪一個instance而定。

一般常用的靜態型別程式語言例如Java、C#,要做到polymorphism必須透過繼承,不管是implementation inheritance或是interface inheritance。有些動態型別程式語言,如果Teddy沒記錯的話像是Smalltalk(如果有錯請留言或來信幫忙更正),可以不需要靠繼承來達到polymorphism,但是在runtime如果接收者沒有支援某個訊息的話,則是會出現runtime errors。

總之,其實Teddy想講的重點是,這些老外厲害的地方,就是可以用很簡短的句子寫出某些觀念或是名詞的定義。因為除非真正了解所要描述的觀念或是名詞,否則是無法寫出這樣的定義出來。當年Teddy背下這個定義(因為考試會考啊…Orz)到現在都還依稀記得。什麼,你問Teddy記下這些定義有何用 ?Teddy也說不上來,也許哪一天鄉民們去面試不小心考了polymorphism這一題,而鄉民們恰巧看到這篇文章所以把書中的定義給寫了出來,但是主考者可能看不懂...Orz。不過這都不是重點,就以「純欣賞」的角度為能夠寫出這麼精簡定義的作者們拍拍手就好了...XD。

友藏內心獨白:這幾天好冷,寫到手都凍僵了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK