У меня есть следующий код для моего перечисления:
public class CoffeeFactory {
public static enum Type {
LONG_BLACK(4.0),
FLAT_WHITE(4.75),
MOCHA(5.5);
private double price;
Type(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
public static enum Ingredient {
ESPRESSO(0.5),
MILK(1),
CHOCOLATE(1.5);
private double cost;
Ingredient(double cost) {
this.cost = cost;
}
public double getCost() {
return cost;
}
}
public static Coffee CreateCoffee(Type type){
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
switch (type) {
case FLAT_WHITE:
ingredients.add(Ingredient.MILK);
Coffee c1 = new Coffee(ingredients, type.FLAT_WHITE);
return c1; //new Coffee(ingredients, Type.FLAT_WHITE);
break;
case LONG_BLACK:
ingredients.add(Ingredient.ESPRESSO);
return new Coffee(ingredients, Type.LONG_BLACK);
break;
case MOCHA:
ingredients.add(Ingredient.CHOCOLATE);
return new Coffee(ingredients, Type.MOCHA);
break;
default:
break;
}
}
}
Это мой код для приготовления кофе:
import patt.Coffee.CoffeeFactory.Ingredient;
public class Coffee {
Type type;
double cost;
ArrayList<Ingredient> ingredients;
public Coffee(ArrayList<Ingredient> ingredients, Type type) {
this.type = type;
this.ingredients = ingredients;
}
public double getCost() {
return cost;
}
public double getPrice() {
return type.ordinal();
}
public String listIngredients() {
return type.toString();
}
}
Ошибка, которую я получаю, находится в строке:
Кофе c1 = новый кофе (ингредиенты, тип FLAT_WHITE);
конструктор Coffee (ArrayList, CoffeeFactory.Type) не определен. Не мог бы кто-нибудь объяснить и показать мне, что я сделал не так, или если я что-то не понимаю.




Линия
Coffee c1 = new Coffee(ingredients, type.FLAT_WHITE);
должно быть один из следующего:
Coffee c1 = new Coffee(ingredients, type);
Coffee c1 = new Coffee(ingredients, Type.FLAT_WHITE);
Вы, наверное, захотите первый. Переменная type уже содержит ссылку на экземпляр перечисления TypeFLAT_WHITE. Вы знаете это, потому что находитесь в этой ветви switch.
В этом нет никакой ошибки.
public static Coffee CreateCoffee(Type type){
ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
switch (type) {
case FLAT_WHITE:
ingredients.add(Ingredient.MILK);
Coffee c1 = new Coffee(ingredients, Type.FLAT_WHITE);
return c1; //new Coffee(ingredients, Type.FLAT_WHITE);
case LONG_BLACK:
ingredients.add(Ingredient.ESPRESSO);
return new Coffee(ingredients, Type.LONG_BLACK);
case MOCHA:
ingredients.add(Ingredient.CHOCOLATE);
return new Coffee(ingredients, Type.MOCHA);
default:
break;
}
return null;
}
Я не использую java, но я предполагаю, что этот тип. FLAT_WHITE использует System.Type или какой-то эквивалент этого вызова вашего перечисления что-то еще