作者TonyQ (骨头)
看板java
标题Re: [问题] abstract 的宣告与 polymorphic 新手问题
时间Tue Apr 24 01:16:31 2007
※ 引述《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