<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">对于Java处理多参数时,先给大家上两个程序员平时最喜爱用的写法:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">第一:重叠构造器</span>
public class NutritionFacts { //重叠构造器 private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public NutritionFacts(int servingSize, int servings) { this(servingSize,servings,0); } public NutritionFacts(int servingSize, int servings, int calories) { this(servingSize,servings,calories,0); } public NutritionFacts(int servings, int servingSize, int caloriesm, int fat) { this(servings,servingSize,caloriesm,fat,0); } public NutritionFacts(int calories, int fat, int servings, int servingSize, int sodium) { this(calories,fat,servings,servingSize,sodium,0); } public NutritionFacts(int calories, int servingSize, int servings, int fat, int sodium, int carbohydrate) { this.calories = calories; this.servingSize = servingSize; this.servings = servings; this.fat = fat; this.sodium = sodium; this.carbohydrate = carbohydrate; } }
NutritionFacts facts = new NutritionFacts(12,34,2,5,3,5);
public class NutritionFactsJavaBean { //JavaBean模式 private int servingSize; private int servings; private int calories; private int fat; private int sodium; private int carbohydrate; public NutritionFactsJavaBean() { } public void setCalories(int calories) { this.calories = calories; } public void setCarbohydrate(int carbohydrate) { this.carbohydrate = carbohydrate; } public void setFat(int fat) { this.fat = fat; } public void setServings(int servings) { this.servings = servings; } public void setServingSize(int servingSize) { this.servingSize = servingSize; } public void setSodium(int sodium) { this.sodium = sodium; } }
NutritionFactsJavaBean factsJavaBean = new NutritionFactsJavaBean(); factsJavaBean.setCalories(34); factsJavaBean.setCarbohydrate(33); factsJavaBean.setFat(333); factsJavaBean.setServings(345);
遇到多参数,推荐使用构建器:
public class NutritionFactsBuilder { //构建器模式 private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder{ private final int servingSize; private final int servings; private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val){ calories = val; return this; } public Builder fat(int val){ fat = val; return this; } public Builder sodium(int val){ sodium = val; return this; } public Builder carbohydrate(int val){ carbohydrate = val; return this; } public NutritionFactsBuilder build(){ return new NutritionFactsBuilder(this); } } private NutritionFactsBuilder(Builder builder){ servings = builder.servings; servingSize = builder.servingSize; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; } }
NutritionFactsBuilder builder = new NutritionFactsBuilder.Builder(12,34) .calories(44) .carbohydrate(55) .fat(456) .sodium(23) .build();