軟件藝術思考者 |
|
|||
混沌,彷徨,立志,蓄勢... |
公告
日歷
導航隨筆分類(86)
隨筆檔案(85)
搜索最新評論
閱讀排行榜評論排行榜 |
適配器模式
適配器模式可以把兩個不相關的類組裝在一起使用。實際上是繼承和組合的綜合運用。 讓我們來看一個例子: public interface Itable{ public void say(String str); } public interface Ichair{ public void say(String str); } public class Table implements? Itable{ public void say(String str){ System.out.println(" i am a table :"+str); } } public class Chair implements Ichair{ public void say(String str){ System.out.println(" i am a chair :"+str); } } public class AdapterFitment? extends Table{ private Ichair c; ? public AdapterFitment(Ichair c){ ? this.c=c; } public void insert(String str){c.say(str);} } 然后呢,這個組合家具就即能當桌子用又能當椅子用了。
評論:
|
![]() |
|
Copyright © 智者無疆 | Powered by: 博客園 模板提供:滬江博客 |
觀音菩薩贊