2016-07-31 119 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