In java enumerations, complex objects are located, while in C ++, each enumeration object is associated with a single integer value. In java, you can have several attributes associated with a single enumeration value:
enum MyCategory { SPORT("The sport category", "sport.png"), NEWS("the news category", "news.jpg"); private String description; private String iconPath; private MyCategory(String description, String iconPath) { this.description = description; this.iconPath = iconPath; } public String getDescription() { return description; } public String getIconPath() { return iconPath; } }
In addition, in java you can switch only types of numbers, strings and enumerations. However, I can not generalize the usual enumerations in general ...
EDIT Another thing java enumerations can do is declare an operation using a value (taken from a java tutorial ):
public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } };
Boris Strandjev Feb 15 '12 at 7:55 2012-02-15 07:55
source share