méthodes - Java support valeurs de paramètres par défaut

Mots clés : javamethodsparametersoverloadingdefault-parametersjava

meilleur 3 Réponses méthodes - Java support valeurs de paramètres par défaut

vote vote

96

Student s1 = new StudentBuilder().name("Eli").buildStudent(); Student s2 = new StudentBuilder()                  .name("Spicoli")                  .age(16)                  .motto("Aloha, Mr Hand")                  .buildStudent(); 
vote vote

84

void foo(String a, Integer b) {     //... }  void foo(String a) {     foo(a, 0); // here, 0 is a default value for b }  foo("a", 2); foo("a"); 
void foo(String a, Integer... b) {     Integer b1 = b.length > 0 ? b[0] : 0;     Integer b2 = b.length > 1 ? b[1] : 0;     //... }  foo("a"); foo("a", 1, 2); 
void foo(String a, Object... b) {     Integer b1 = 0;     String b2 = "";     if (b.length > 0) {       if (!(b[0] instanceof Integer)) {            throw new IllegalArgumentException("...");       }       b1 = (Integer)b[0];     }     if (b.length > 1) {         if (!(b[1] instanceof String)) {              throw new IllegalArgumentException("...");         }         b2 = (String)b[1];         //...     }     //... }  foo("a"); foo("a", 1); foo("a", 1, "b2"); 
void foo(String a, Integer b, Integer c) {     b = b != null ? b : 0;     c = c != null ? c : 0;     //... }  foo("a", null, 2); 
void foo(String a, Optional<Integer> bOpt) {     Integer b = bOpt.isPresent() ? bOpt.get() : 0;     //... }  foo("a", Optional.of(2)); foo("a", Optional.<Integer>absent()); 
 class Foo {      private final String a;       private final Integer b;       Foo(String a, Integer b) {        this.a = a;        this.b = b;      }       //...  }   class FooBuilder {    private String a = "";     private Integer b = 0;     FooBuilder setA(String a) {      this.a = a;      return this;    }     FooBuilder setB(Integer b) {      this.b = b;      return this;    }     Foo build() {      return new Foo(a, b);    }  }   Foo foo = new FooBuilder().setA("a").build(); 
void foo(Map<String, Object> parameters) {     String a = "";      Integer b = 0;     if (parameters.containsKey("a")) {          if (!(parameters.get("a") instanceof Integer)) {              throw new IllegalArgumentException("...");         }         a = (String)parameters.get("a");     } else if (parameters.containsKey("b")) {          //...      }     //... }  foo(ImmutableMap.<String, Object>of(     "a", "a",     "b", 2,      "d", "value"));  
vote vote

76

void MyParameterizedFunction(String param1, int param2, bool param3=false) {} 
void MyParameterizedFunction(String param1, int param2, Boolean... params) {     assert params.length <= 1;     bool param3 = params.length > 0 ? params[0].booleanValue() : false; } 
new Boolean[]{} 
void MyParameterizedFunction(String param1, int param2, bool param3=false, int param4=42) {} 
void MyParameterizedFunction(String param1, int param2, Object... p) {     int l = p.length;     assert l <= 2;     assert l < 1 || Boolean.class.isInstance(p[0]);     assert l < 2 || Integer.class.isInstance(p[1]);     bool param3 = l > 0 && p[0] != null ? ((Boolean)p[0]).booleanValue() : false;     int param4 = l > 1 && p[1] != null ? ((Integer)p[1]).intValue() : 42; } 

Questions similaires