看板 Soft_Job 關於我們 聯絡資訊
當要教純新人時 我大概會這樣瞎扯 1. 封裝 (encapsulation) func step1(step1_params:step1_params_type) { /* do step1 */ } func step2(step2_params:step2_params_type) { /* do step2 */ } ... func give_me_f_result() -> result_type { /* kind of encapsulation */ step1(params1) step2(params2) ... return result } 啥? 你說這樣不就只是個function call? 是,但你把他"包裝"起來後 class IDontGiveADamn { private step1(...) { ... } ... public give_me_f_result() -> result_type { ... } } 使用這個 IDontGiveADamn 的人就不用看到 step1 ~ stepN 的東西了 2. 繼承 (inheritance)=> 原意其實為「擴展 (extend)」較恰當 純這樣解釋應該就蠻夠了,我猜原本的用意狀況是這樣 class CalculateStudentScore { private func stuff_you_dont_care(...) { ... } ... public func result() -> float { ... } } 只要呼叫CalculateStudentScore.result()就可以取得成績了 啊幹,結果教務處跟你講說評分太低,必須用square root*10 你改了 結果隔了1個月跟你講說全部的人都sqrt*10太高了,要你再改另一個運算 你又改了 結果軟體上路1周後教務處再跟你要上述三種不同的成績 並且不能異動到當前版本 於是只好這樣改 class UAsHoes private_extends CalculateStudentScore { public func here_is_you_f_result() { ... } } 這樣你不用改動原本的code,還是可以提供給人渣,啊不,他人使用 3. 介面 (interface)/多型 (polymorphism) 印象中多型好像沒有限定oop,我這邊先用我的唬爛例子 class TellMeAboutSomeone { private func steal_personal_data(person:Americano) { ... } public func callme(person:Americano) { ... } } Ya, if you're an American, this class tell me your info (and steal it). How about Chinese? So you do this: class TellMeAboutSomeone { private func steal_personal_data(person:Americano) { ... } public func callme(person:Americano) { ... } private func steal_personal_data(person:Chinamen) { ... } public func callme(person:Chinamen) { ... } } 好,那我要法文呢? Je crois que vous comprenez où le problème est (powered by Google Translate 為了把 TellMeAboutSomeone 簡化,那就乾脆這麼做吧 class TellMeAboutSomeone { private func steal_personal_data(person:IPerson) { ... } private func callme(person: IPerson) { ... } } interface IPerson { func secret_to_steal() -> Secret; func spell_your_name() -> Name; } 這樣你就輕鬆啦,讓想用你這個class的人去遵守規則並煩惱! ---- 所以oop的好處到底在哪? ...我到現在的感覺還是個從"方便分類"出發的概念XD 當程式越寫越大時,global function與variable命名會越痛苦 有點像是某個謠傳說很久以前John是個菜市場名,喊聲John一堆人會回頭 所以就出現了姓氏來"方便分辨" 很多時候你也不需要知道這個人(物件)私底下到底在做什麼 你只是需要少數的資訊,更可能只是單一的結果(像是送信) 至於更專業的講法,不要問我,我也講了這是給新人的唬爛文 被唬住的人無論是放棄還是跟隨碼農這條不歸路我都無所謂就是XD -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 211.20.17.50 ※ 文章網址: https://www.ptt.cc/bbs/Soft_Job/M.1510627356.A.67A.html
Vick753: 請問fun()->XX 是什麼意思阿 11/14 11:38
dreamnook: 函式 某種語言的慣用法 用c來說是type funcName() 11/14 11:48