嗨JAVA板的大家
想問一個奇妙的結構問題
簡單來說我想定義一個幫忙抓資料跟傳資料上去的interface
(用Android, 但我想這個問題應該是JAVA通用)
所以我想定義一個簡單的interface 裡面可以設定URL 傳資料跟抓資料
簡化版是這樣:
interface A{
public void setURL(String str);
public int getInt(); //get int from server
public void pushInt(int i); //push int to server
}
//當然其實get跟push參數更複雜這邊我刻意簡化以聚焦問題
但是設計的時候我料想 implements A的class應該在constructor塞URL的String進去
如下所示:
A a = new A_i(urlString);// A_i implements A
// no need to a.setURL(urlString)
a.pushInt(123);
所以其實不用定義setURL這個東西
但是不知道要怎樣寫 才能規範implements interface的時候
一定要有一個constructor是吃一個String參數的
以確保這個A被implements時style的一致性
目標是在生成javadoc的時候 在interface A的頁面很清楚讓使用者知道
URL就是constructor的時候塞進去的 你用哪個implement的class都一樣
感覺很直觀又酷 不知道有沒有辦法
謝謝大家