标签:pac cti color get 不一致 新特性 卡路里 val ret
创建Java对象时,对于可为空的属性,创建对象的时候有3种模式:重叠构造器模式、JavaBeans模式、Builder模式(推荐)、Stream模式(推荐)。
package effectiveJava.builder; public class NutritionFactsMultiContr { //食物尺寸 private int servingSize; //食物数量 private int servings; //热量(卡路里) private int calories; //脂肪含量 private int fat; //食用盐(钠)含量 private int sodium; //糖类含量 private int carbohydrate; public NutritionFactsMultiContr(int servingSize, int servings) { this(servingSize,servings,0); } public NutritionFactsMultiContr(int servingSize, int servings, int calories) { this(servingSize,servings,calories,0); } public NutritionFactsMultiContr(int servingSize, int servings, int calories, int fat) { this(servingSize,servings,calories,fat,0); } public NutritionFactsMultiContr(int servingSize, int servings, int calories, int fat, int sodium) { this(servingSize,servings,calories,fat,sodium,0); } public NutritionFactsMultiContr(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) { this.servingSize = servingSize; this.servings = servings; this.calories = calories; this.fat = fat; this.sodium = sodium; this.carbohydrate = carbohydrate; } public static void main(String[] args) { NutritionFactsMultiContr nutritionFacts = new NutritionFactsMultiContr(1,2,3,4,5,6); } }
package effectiveJava.builder; //营养成分 public class NutritionFactsSetter { //食物尺寸 private int servingSize; //食物数量 private int servings; //热量(卡路里) private int calories; //脂肪含量 private int fat; //食用盐(钠)含量 private int sodium; //糖类含量 private int carbohydrate; public void setServingSize(int servingSize) { this.servingSize = servingSize; } public void setServings(int servings) { this.servings = servings; } public void setCalories(int calories) { this.calories = calories; } public void setFat(int fat) { this.fat = fat; } public void setSodium(int sodium) { this.sodium = sodium; } public void setCarbohydrate(int carbohydrate) { this.carbohydrate = carbohydrate; } public static void main(String[] args) { NutritionFactsSetter nutritionFacts = new NutritionFactsSetter(); nutritionFacts.setCalories(1); nutritionFacts.setCarbohydrate(2); } }
package effectiveJava.builder; //营养成分 public class NutritionFacts { //食物尺寸 private int servingSize; //食物数量 private int servings; //热量(卡路里) private int calories; //脂肪含量 private int fat; //食用盐(钠)含量 private int sodium; //糖类含量 private int carbohydrate; /** * NutritionFacts是不可变的,不对外提供构造器 * @param builder */ private NutritionFacts(Builder builder) { this.servingSize = builder.servingSize; this.servings = builder.servings; this.calories = builder.calories; this.fat = builder.fat; this.sodium = builder.sodium; this.carbohydrate = builder.carbohydrate; } public static class Builder { private int servingSize; private int servings; private int calories; private int fat; private int sodium; private int carbohydrate; //必填字段 public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } //可选字段 public Builder calories(int val){ this.calories = val; return this; } public Builder fat(int val){ this.fat = val; return this; } public Builder sodium(int val){ this.sodium = val; return this; } public Builder carbohydrate(int val){ this.carbohydrate = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } public static void main(String[] args) { NutritionFacts facts = new NutritionFacts.Builder(1, 2).calories(3).fat(4).build(); } }
package effectiveJava.builder; public class NutritionFactsStream { //食物尺寸 private int servingSize; //食物数量 private int servings; //热量(卡路里) private int calories; //脂肪含量 private int fat; //食用盐(钠)含量 private int sodium; //糖类含量 private int carbohydrate; public NutritionFactsStream() { } public int getServingSize() { return servingSize; } public NutritionFactsStream setServingSize(int servingSize) { this.servingSize = servingSize; return this; } public int getServings() { return servings; } public NutritionFactsStream setServings(int servings) { this.servings = servings; return this; } public int getCalories() { return calories; } public NutritionFactsStream setCalories(int calories) { this.calories = calories; return this; } public int getFat() { return fat; } public NutritionFactsStream setFat(int fat) { this.fat = fat; return this; } public int getSodium() { return sodium; } public NutritionFactsStream setSodium(int sodium) { this.sodium = sodium; return this; } public int getCarbohydrate() { return carbohydrate; } public NutritionFactsStream setCarbohydrate(int carbohydrate) { this.carbohydrate = carbohydrate; return this; } public static void main(String[] args) { NutritionFactsStream nutritionFacts = new NutritionFactsStream().setServings(1).setServings(2); } }
此外,创建对象的模式还有很多,比如:工厂模式、单例模式等,不是本文讨论的重点,有兴趣的可以自己研究一下。
参考资料:
标签:pac cti color get 不一致 新特性 卡路里 val ret
原文地址:https://www.cnblogs.com/BlueStarWei/p/13335632.html