Re: [請益] 我這樣解釋OOP對嗎?

作者: dreamnook (亞龍)   2017-11-14 10:42:33
當要教純新人時
我大概會這樣瞎扯
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的人去遵守規則並煩惱!
作者: Vick753 (彬彬)   2017-11-14 11:38:00
請問fun()->XX 是什麼意思阿
作者: dreamnook (亞龍)   2017-11-14 11:48:00
函式 某種語言的慣用法 用c來說是type funcName()

Links booklink

Contact Us: admin [ a t ] ucptt.com