如何从接口获取枚举到实现该接口的 class?
How can I get an enum from an interface to a class that implements that interface?
我正在尝试从此接口获取枚举:
public interface PizzaInterface {
public enum Toppings {
pepperoni, sausage, mushrooms, onions, greenPeppers;
}
}
对此class:
public class Pizza implements PizzaInterface{
private String[] toppings = new String[5];
}
并能存入数组
(编辑):
如果有任何改变,我想把它放在 ArrayList 中。
为什么要 String[]
? Toppings[]
会更好。你可以用
来做到这一点
PizzaInterface.Toppings[] toppings = PizzaInterface.Toppings.values();
如果您想将值存储为字符串,您可以这样做:
private String[] toppings = names();
public static String[] names() {
Toppings[] toppings = PizzaInterface.Toppings.values();
String[] names = new String[toppings.length];
for (int i = 0; i < toppings.length; i++) {
names[i] = toppings[i].name();
}
return names;
}
否则只需从您的枚举中调用 .values() 方法,您将获得一组 Toppings
PizzaInterface..Toppings.values();
您需要了解的第一件事是 Enum 在该接口内将是静态的。并且在任何枚举上调用 values()
方法将 return 枚举实例数组。因此,如果您可以使用 Enum 数组而不是 String,您应该像上面提到的 pbabcdefp 那样使用 values()
调用。 :
PizzaInterface.Toppings[] toppings = PizzaInterface.Toppings.values();
但是如果你需要String内容,我建议你使用ArrayList。使用 ArrayList 通常比使用 Arrays 有更多的好处。在那种情况下,如果我是你,我会在 Enum class 中添加一个静态方法到 return 字符串列表,我会在 Pizza class 中使用它。示例代码如下:
public interface PizzaInterface {
public enum Toppings {
pepperoni, sausage, mushrooms, onions, greenPeppers;
public static List<String> getList(){
List<String> toppings=new ArrayList<String>();
for (Toppings topping:Toppings.values() ){
toppings.add(topping.name());
}
return toppings;
}
}
}
和
public class Pizza implements PizzaInterface{
private static List<String> toppings = PizzaInterface.Toppings.getList();
//use the toppings list as you want
}
我正在尝试从此接口获取枚举:
public interface PizzaInterface {
public enum Toppings {
pepperoni, sausage, mushrooms, onions, greenPeppers;
}
}
对此class:
public class Pizza implements PizzaInterface{
private String[] toppings = new String[5];
}
并能存入数组
(编辑): 如果有任何改变,我想把它放在 ArrayList 中。
为什么要 String[]
? Toppings[]
会更好。你可以用
PizzaInterface.Toppings[] toppings = PizzaInterface.Toppings.values();
如果您想将值存储为字符串,您可以这样做:
private String[] toppings = names();
public static String[] names() {
Toppings[] toppings = PizzaInterface.Toppings.values();
String[] names = new String[toppings.length];
for (int i = 0; i < toppings.length; i++) {
names[i] = toppings[i].name();
}
return names;
}
否则只需从您的枚举中调用 .values() 方法,您将获得一组 Toppings
PizzaInterface..Toppings.values();
您需要了解的第一件事是 Enum 在该接口内将是静态的。并且在任何枚举上调用 values()
方法将 return 枚举实例数组。因此,如果您可以使用 Enum 数组而不是 String,您应该像上面提到的 pbabcdefp 那样使用 values()
调用。 :
PizzaInterface.Toppings[] toppings = PizzaInterface.Toppings.values();
但是如果你需要String内容,我建议你使用ArrayList。使用 ArrayList 通常比使用 Arrays 有更多的好处。在那种情况下,如果我是你,我会在 Enum class 中添加一个静态方法到 return 字符串列表,我会在 Pizza class 中使用它。示例代码如下:
public interface PizzaInterface {
public enum Toppings {
pepperoni, sausage, mushrooms, onions, greenPeppers;
public static List<String> getList(){
List<String> toppings=new ArrayList<String>();
for (Toppings topping:Toppings.values() ){
toppings.add(topping.name());
}
return toppings;
}
}
}
和
public class Pizza implements PizzaInterface{
private static List<String> toppings = PizzaInterface.Toppings.getList();
//use the toppings list as you want
}