[英]Undefined Constructor Enum Java
I have the following code for my enum: 我的枚举有以下代码:
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;
}
}
}
This is my code for creating a coffee: 这是我制作咖啡的代码:
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();
}
}
The error im getting is in the line: 我得到的错误是在行中:
Coffee c1 = new Coffee(ingredients, type.FLAT_WHITE); 咖啡c1 =新咖啡(成分,类型:FLAT_WHITE);
the constructor Coffee(ArrayList, CoffeeFactory.Type) is undefined. 构造函数Coffee(ArrayList,CoffeeFactory.Type)是未定义的。 Could someone please explain and show me what I have done wrong or if I'm misunderstanding something. 有人可以解释一下,告诉我我做错了什么,或者如果我误解了什么。
The line 线
Coffee c1 = new Coffee(ingredients, type.FLAT_WHITE);
should be one of the following: 应为下列之一 :
Coffee c1 = new Coffee(ingredients, type);
Coffee c1 = new Coffee(ingredients, Type.FLAT_WHITE);
You probably want the first one. 您可能想要第一个。 The variable type
already contains a reference to the Type
enum instance FLAT_WHITE
. 变量type
已经包含对Type
枚举实例FLAT_WHITE
。 You know this because you're in that branch of the switch
. 您之所以知道这一点,是因为您在switch
那个分支中。
There is no mistake in this. 没有错。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.