Replace int constant with enum
•
Java
Benefits of enumeration:
1. Type safety
2. Ease of use
3),BLUE(5),BLACK(8),YELLOW(13),GREEN(28 Color(.colorValue= </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> value(){ </span><span style="color: #0000ff;">return</span> ordinal()+1<span style="color: #000000;">; }
} public static void main(String[] args) { for (Color s : Color.values()) { //< span style="color: #008000;"> Enum's values () returns an array, which is seasons [] system out. println(s.value()+":"+s.name()+"="+ s.getColorValue()); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码