我找到了Joshua Bloch建造者模式,其着名的NutritionFacts例子。现在我希望这个构建器构建两种类型的NutritionFacts。所以我们应该说而不是:
NutritionFacts cocaCola = new NutritionFacts.Builder().
calories(100).sodium(35).carbohydrate(27).build();
我想有类似的东西:
NutritionFactsA a = new NutritionFacts.Builder<NutritionFactsA>...().
calories(100).build(); //.sodium(35).carbohydrate(27) - these are not possible to be set, when I write NutritionFactsA I cannot see these parameters
NutritionFactsB b = new NutritionFacts.Builder<NutritionFactsB>(240, 8).
.sodium(35).build(); //here I have set only sodium value, I cannot set anything
Java7中有可能这样做吗?