Java 如何从接口获取实现该接口的类的枚举?
我正在尝试从此接口获取枚举:Java 如何从接口获取实现该接口的类的枚举?,java,interface,enums,Java,Interface,Enums,我正在尝试从此接口获取枚举: public interface PizzaInterface { public enum Toppings { pepperoni, sausage, mushrooms, onions, greenPeppers; } } 对于这一类: public class Pizza implements PizzaInterface{ private String[] toppings = new String[5]; } 并
public interface PizzaInterface {
public enum Toppings {
pepperoni, sausage, mushrooms, onions, greenPeppers;
}
}
对于这一类:
public class Pizza implements PizzaInterface{
private String[] toppings = new String[5];
}
并能够将其存储在阵列中
(编辑):
如果有什么变化,我想把它放在一个数组列表中。为什么需要一个
字符串[]
?AToppings[]
会更好。你可以用它来做这个
PizzaInterface.Toppings[] toppings = PizzaInterface.Toppings.values();
为什么需要
字符串[]
?AToppings[]
会更好。你可以用它来做这个
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;
}
如果要将值存储为字符串,可以执行以下操作:
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;
}
您需要了解的第一件事是,Enum在该接口内是静态的。对任何枚举调用
values()
方法将返回枚举实例数组。因此,如果您可以使用枚举数组而不是字符串,那么应该像上面提到的pbabcdefp那样使用values()
调用:
PizzaInterface.Toppings[]Toppings=PizzaInterface.Toppings.values()代码>
但如果您需要字符串内容,我建议您使用ArrayList。使用ArrayList通常比使用数组有更多的好处。在这种情况下,如果我是您,我会在Enum类中添加一个静态方法来返回字符串列表,我会在Pizza类中使用它。示例代码如下所示:
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;
}
}
公共界面PizzaInterface{
公共枚举浇头{
香肠、香肠、蘑菇、洋葱、青椒;
公共静态列表getList(){
列表顶部=新的ArrayList();
对于(浇头浇头浇头:Toppings.values()){
toppings.add(topping.name());
}
返回浇头;
}
}
}
及
公共类Pizza实现Pizza接口{
私有静态列表toppings=PizzaInterface.toppings.getList();
//根据需要使用浇头列表
}您需要了解的第一件事是,Enum在该接口内是静态的。对任何枚举调用values()
方法将返回枚举实例数组。因此,如果您可以使用枚举数组而不是字符串,那么应该像上面提到的pbabcdefp那样使用values()
调用:
PizzaInterface.Toppings[]Toppings=PizzaInterface.Toppings.values()代码>
但如果您需要字符串内容,我建议您使用ArrayList。使用ArrayList通常比使用数组有更多的好处。在这种情况下,如果我是您,我会在Enum类中添加一个静态方法来返回字符串列表,我会在Pizza类中使用它。示例代码如下所示:
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;
}
}
公共界面PizzaInterface{
公共枚举浇头{
香肠、香肠、蘑菇、洋葱、青椒;
公共静态列表getList(){
列表顶部=新的ArrayList();
对于(浇头浇头浇头:Toppings.values()){
toppings.add(topping.name());
}
返回浇头;
}
}
}
及
公共类Pizza实现Pizza接口{
私有静态列表toppings=PizzaInterface.toppings.getList();
//根据需要使用浇头列表
}