從你的問題你已經指出,在Constants.java
你有下面的語句:
private static String BASE_URL = "http://www.website.com/";
當你考,你試圖改變這種BASE_URL
,你不能這樣做。它不僅是一個private
成員或Constants
,但它也是一個final
成員,這意味着它的價值不能改變。
你可以做的是創建實例化Constants
當你的應用程序運行,你可以指定它是一個TEST
或RELEASE
版本。
像下面這樣可能就足夠了:
public enum RUN_TYPE {
TEST,
RELEASE;
}
您可以通過修改Constants
類是類似以下內容:
public class Constants {
private static final instance = new Constants();
private RUN_TYPE type;
private String BASE_URL;
// More urls here
private Constants(){
}
public void setRunType(RUN_TYPE type){
this.type = type;
if(type == RUN_TYPE.RELEASE){
BASE_URL = "http://release.api/endpoints";
} else if(type == RUN_TYPE.TEST){
BASE_URL = "http://test.api/endpoints";
}
}
public String getBaseUrl(){
return BASE_URL;
}
public static Constants getInstance(){
return instance;
}
// More getters here
}
這是一個單例類將包含所需的值如您所述。
在釋放模式中,你可以撥打:
Constants.getInstance().setRunType(RUN_TYPE.RELEASE);
雖然測試你可以這樣做:
@RunWith(AndroidJUnit4.class)
public class TestSomething {
@BeforeClass
public static void runOnce(){
Constants.getInstance().setRunType(RUN_TYPE.TEST);
}
@Test
public void testOne(){
// tests
}
}
什麼意思由_apparently_做?如果您發佈代碼以便我們可以看到發生了什麼,這將會很有幫助。 – px06
您可以將參數傳遞給主方法並由其解析。相關問題在這裏:http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java –
@ px06由顯然,我做aha。我沒有代碼張貼因爲我不知道該怎麼做! – BilalMH