The best way to compare enums
This question already has an answer here:
Use ==
. There cannot be multiple instances of the same enum constant (within the context of a classloader, but let's ignore that point) so it's always safe.
That said, using equals()
is safe too, and will perform reference equality as well. It's pretty much a style choice.
Personally I very seldom find myself using if
statements for enums at all. I favour switch
blocks.
switch (c1) {
case Brown:
//is brown
break;
case Red:
//...
}
链接地址: http://www.djcxy.com/p/91826.html
上一篇: 枚举equals()和==
下一篇: 比较枚举的最好方法