java - How can I get an enum from an interface to a class that implements that interface? -


i trying enum interface:

public interface pizzainterface {     public enum toppings {         pepperoni, sausage, mushrooms, onions, greenpeppers;     } } 

to class:

public class pizza implements pizzainterface{     private string[] toppings = new string[5]; } 

and able store in array.

(edit): want put in arraylist if changes anything.

the first thing need understand enum static inside interface. , call values() method on enum return array of enums instances. if can work enum array rather string, should using values() call way pbabcdefp mentioned above. :

pizzainterface.toppings[] toppings = pizzainterface.toppings.values();

but if need string contents, suggest use arraylist. there more benefits using arraylist arrays. in case , if you, add 1 static method inside enum class return list of strings, have used in pizza class. sample code :

public interface pizzainterface { public enum toppings {     pepperoni, sausage, mushrooms, onions, greenpeppers;     public static list<string> getlist(){        list<string> toppings=new arraylist<string>();        (toppings topping:toppings.values() ){            toppings.add(topping.name());        }        return toppings;    } } 

}

and

public class pizza implements pizzainterface{    private static list<string> toppings = pizzainterface.toppings.getlist(); //use toppings list want 

}


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -