2017-04-21 51 views
1

當我使用該方法時,是否有任何可能的方法來推薦參數? (就像註釋?)如何在java中推薦參數?

我做了很多靜態最終的字符串參數爲url連接,我想知道我是否使用正確的輸入參數或者當我代碼。

下面是我的例子。

public void myRequest(String inputParameter) { 
    String requestURL = ""; 

    static final String ex1 = "http://example.com/xml1"; 
    static final String ex2 = "http://example.com/xml2"; 
    static final String ex3 = "http://example.com/xml3"; 
    static final String ex4 = "http://example.com/xml4"; 
    static final String ex5 = "http://example.com/xml5"; 
    static final String ex6 = "http://example.com/xml6"; 
    static final String ex7 = "http://example.com/xml7"; 
    // too many.. 
    // .... 
    static final String ex125 = "http://example.com/xm125"; 

    if(inputParameter.equals("ex1")) { 
     requestURL = ex1; 
    } else if(inputParameter.equals("ex2")) { 
     requestURL = ex2; 
    } 
    // just like this.. 
    // ..... 
     else if(inputParameter.equals("ex125")) { 
     requestURL = ex125; 
    } 
    String requestURL = inputParameter; 

    URL url = new URL(requestURL); 
    URLConnection urlConnection = url.openConnection(); 
    // below codes are unnecessary. 
} 

,我會用這種方法,在「主」的方法

public static void main(String[] args) { 
    myRequest("ex1"); // this!! 
} 

的一點是,當我寫的方法「myRequest()」,IDE會告訴我有關參數的信息(當光標位於'('和')'之間時)。唯一可以注意到的是「你應該寫String對象」。不是「你可以使用ex1這意味着加載ex1.xml,ex2意味着ex2.xml或... ex125意思是xm125.xml」

如果我的希望成真,結果就像是這樣。

(寫作方法)

myRequest(|); // there is cursor between (and) 

「我應該要求什麼XML?嗯..還有EX1到ex125。行.. EX1意味着..我的家鄉歷史.. EX2意味着我校的歷史。 。和嗯..我應該用ex4 ok!「 (我下面寫的方法)

myRequest("ex4"); 

我想IDE通知我字符串參數我應該使用什麼。

有什麼可能的辦法嗎?

+0

@ScaryWombat但是,怎麼樣?我認爲IDE不知道該靜態最終的字符串變量是否與輸入參數有關係... – TyeolRik

+0

@GhostCat對不起,我工作遲到。你能查看我的新增範例嗎? – TyeolRik

+0

@GhostCat 嗯,我不知道,因爲我在這裏是新手。我所做的是點擊你的答案旁邊的複選框LOL – TyeolRik

回答

1

考慮到您對問題的更新,答案是圍繞enumsmaps構建的。換句話說:你簡單地說,永遠不會放下像你那樣的常量列表;然後「映射」(通過代碼中的硬連線;如您在示例中那樣)到其他一些傳入字符串。

取而代之:您可以使用枚舉來保存這些常量;並且您還可以向該枚舉類添加一些方法,該方法知道如何將傳入字符串映射到可用的枚舉常量。

但重點是:你想放棄「原始」字符串。 IDE(分別爲編譯器)無法幫助您添加「ext1」字符串作爲方法參數。

但是如果你有:

public enum ExUrls { 
EX1("http://example.com/xml1"), EX2("... 
... a private constructor that takes that url string) 

那麼你做的事:

void someMethod(ExURls ex) { 

,突然的,IDE將能夠在所有潛在ExUrl常量建議你!

0

我不確定如果我的問題沒有問題,但可以使用javadocs。

這是oracle的一個例子。

/** 
* Returns an Image object that can then be painted on the screen. 
* The url argument must specify an absolute {@link URL}. The name 
* argument is a specifier that is relative to the url argument. 
* <p> 
* This method always returns immediately, whether or not the 
* image exists. When this applet attempts to draw the image on 
* the screen, the data will be loaded. The graphics primitives 
* that draw the image will incrementally paint on the screen. 
* 
* @param url an absolute URL giving the base location of the image 
* @param name the location of the image, relative to the url argument 
* @return  the image at the specified URL 
* @see   Image 
*/ 
public Image getImage(URL url, String name) { 
     try { 
      return getImage(new URL(url, name)); 
     } catch (MalformedURLException e) { 
      return null; 
     } 
} 

所有你需要做的是類型/ **,然後按你的函數,那麼你可以添加細節的頂部進入。這些將在您懸停到其函數調用時顯示。

在你的情況可能是這樣

 /** 
    * This method is very amazing it will cure cancer 
    * (ex1 - hometown history), 
    * (ex2 - school history), 
    * (ex3 - blah blah), 
    * (ex4 - what is the meaning of life), 
    * ... 
    * (ex125 - choose this) 
    * 
    * @param inputParameter - (String) You can input ex1 up to ex125 
    */ 
    public void myRequest(String inputParameter) { 

,但如果你希望限制輸入到EX1-ex125只需要別的東西。

+0

謝謝您幫助我,但是,您可以在後期檢查我新添加的示例嗎?我認爲我想要做的事情存在某種誤解。 – TyeolRik

+0

如果它只是編碼指南,通常使用Javadocs。 試着把這個註釋放在上面public void myRequest(String inputParameter){ – cebuseiran