2016-07-31 116 views
1

我很難理解這段代碼。我不確定包含註釋的行是否聲明瞭一個方法。我嘗試了Google列表方法,但很遺憾沒有找到任何東西。謝謝:)Java列表方法聲明?

List<String> getBrands(String color) {//I don't understand this line of code 
     List<String> brands = new ArrayList<String>(); 
     if(color.equals("amber")) { 
      brands.add("Jack Amber"); 
      brands.add("Red Moose"); 
     } else { 
      brands.add("Jail Pale Ale"); 
      brands.add("Gout Stout"); 
     } 
     return brands; 
    } 

} 
+0

簡單 - 'getBrands'是返回一個'列表的方法'(讀 - 字符串列表) – nullpointer

回答

3

聲明它的返回類型List<String>的方法,字符爲泛型類型列表中。

+1

返回類型'名單' – nullpointer

+0

對於某些原因,移動網站不會讓我放置任何更大或更小的圖標。但是,是的。 – Recips

+0

如果方法沒有被聲明爲public,它是否默認設置爲protected? – Moonear

2

在設計方法時,您需要知道以下零件

public static void myMethod(int parameter) throws someException { 
    //method body 
} 
  1. 訪問修飾符(公共)
  2. 可選符(靜態)
  3. 返回類型(無效)
  4. 方法名(myMethod的)
  5. 參數列表(INT參數)
  6. 可選異常(拋出someException)
  7. 方法體({//方法體)

注::訪問修飾符,可選符和可選的例外是可選。其他人需要

在你的代碼,

List<String> getBrands(String color) { 
    // method body 
} 

/* 

Your access modifier is default (no declaration) 
List<String> is return type 
getBrands is method name 
(String color) is parameter list 
{ // .... } is method body 

*/ 
+0

謝謝你清楚地解釋它。不勝感激 :) – Moonear