Replace int constant with enum

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;"&gt;private</span> <span style="color: #0000ff;"&gt;int</span><span style="color: #000000;"&gt; value(){ </span><span style="color: #0000ff;"&gt;return</span> ordinal()+1<span style="color: #000000;"&gt;; }

} 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
分享
二维码
< <上一篇
下一篇>>