※ 引述《neigence (心夜)》之銘言:
: 這樣子就可以發生多型了
: 在printshape裡面 不用判斷ss是那一種子class
: 不用
: if(ss instanceof Circle)
: else if(ss instanceof Triangle)
: 程式碼可以變的容易讀 跟 擴充
多型的經典例子,
個人認為應該算是裝飾者模式和狀態模式吧。
Decorater Pattern , state pattern .
最主要的是通用型態去處理共同的問題。(?)
裝飾者就不用說了,透過包裝增加功能。
狀態模式,則是在不同的狀態對事情的處理跟著做狀態性的改變。
---
當然 , 要寫很多個if-else 不會不行,(原文說的a1.print())
不過這也是個有趣的方式~:P
--
String temp="relax"; | Life just like programing
while(buringlife) String.forgot(temp); | to be right or wrong
while(sleeping) brain.setMemoryOut(); | need not to say
stack.push(life.running); | the compiler will
stack.push(scouting.buck()); | answer your life
stack.push(bowling.practice()); | Bone everything
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.27.68