Java 枚举:覆写 toString,再看 equals、hashCode
来源:互联网 发布:淘宝店什么最好卖 编辑:程序博客网 时间:2024/06/05 19:45
仍然采用 Java 枚举:理解枚举本质 例子。稍加修改。
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {System.out.println(color);}}public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}}}
打印结果
原本以为会打印如下形式的东西
但是,实际情况不是这样。具体可以查看 Enum 的源码,看看 toString 方法:
/** * Returns the name of this enum constant, as contained in the * declaration. This method may be overridden, though it typically * isn't necessary or desirable. An enum type should override this * method when a more "programmer-friendly" string form exists. * * @return the name of this enum constant */ public String toString() {return name; }
注释写的很明白,返回的是 the name of enum constant(枚举常量值的名字)。
如果我们想要得到如期的结果,需要重写 toString 方法。
package mark.demo;public class EnumDemo {public static void main(String[] args) {for (Color color : Color.values()) {System.out.println(color);}}public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}@Overridepublic String toString() {return _name + "," + _id;}}}
上面代码里面使用增强 for 循环调用 values() 方法,遍历枚举的值,也可以这样来遍历:
for (Color color : Color.values()) {System.out.println(color.getName() + "," + color.getId());}
Enum 类的 equals、hashCode 方法
/** * Returns true if the specified object is equal to this * enum constant. * * @param other the object to be compared for equality with this object. * @return true if the specified object is equal to this * enum constant. */ public final boolean equals(Object other) { return this==other; } /** * Returns a hash code for this enum constant. * * @return a hash code for this enum constant. */ public final int hashCode() { return super.hashCode(); }
都是 final 的,很明显,设计者不想让子类去重写这两个方法。
的确,在枚举里面无法重写这两个方法。但是如何使用这个方法呢?
现在,我们不去重写 toString 方法,看看 equals 效果。
public enum Color {RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3);Color(String name, int id) {_name = name;_id = id;}private String _name;private int _id;public String getName() {return _name;}public int getId() {return _id;}}
public static void main(String[] args) {System.out.println(Color.RED.equals(""));// falseSystem.out.println(Color.RED.equals("red color"));// falseSystem.out.println(Color.RED.equals("RED"));// falseSystem.out.println(Color.RED.equals(Color.BLUE));// false// 同一个对象System.out.println(Color.RED.equals(Color.RED));// true// 实际是两个 StringSystem.out.println(Color.RED.toString().equals("RED"));// true// 实际是两个 StringSystem.out.println(Color.RED.getName().equals("red color"));// true// hashCodeSystem.out.println("Color.RED.hashCode = " + Color.RED.hashCode());}
如果重写了类似上面的 toString 方法,那么
System.out.println(Color.RED.toString().equals("RED"));
如下面这种实现方法:返回 _name 的大写形式。
@Overridepublic String toString() {return this._name.toUpperCase();}
综上,建议根据需要可以重写 toString 方法,善用 equals 方法。
- Java 枚举:覆写 toString,再看 equals、hashCode
- Java枚举:覆写toString,再看equals、hashCode
- Java toString equals hashCode 方法的重写
- java继承中的 equals + hashCode+toString
- Java equals() ,hashCode(),toString() 的推荐写法。
- equals, hashCode,toString
- equals() 、 toString() 、 getClass() 、 hashCode()
- equals&&hashCode&&toString函数
- equals()、hashCode()、toString()
- equals hashcode toString
- java解惑之重载与覆写(equals与hashCode) .
- 如何覆写java中的equals和hashcode方法
- Java Object类 hashcode、 equals 、 toString、notify()、notifyAll()、finalize()
- Java学习之equals()、hashCode()、identityHashCode()、toString()和==
- 【Core Java Volume1】重写equals,hashCode,toString方法
- Java记录 -57- Object的equals、hashcode和toString方法
- Object 类 equals toString hashCode
- equals、hashCode、toString 方法重写
- 使用struts2+spring+tomcat+mysql数据库开发中文乱码问题解决方案
- perl,php程序只运行一个
- Read Image
- AS3.0 性能优化官方文档
- 搜狗输入法,无心插柳柳成荫
- Java 枚举:覆写 toString,再看 equals、hashCode
- Linux 信号机制分析
- POJ 2155 - Matrix
- uusee花4000万买个奥运转播权
- ListView双缓冲!
- Linux卸载旧内核方法
- 中移动为何惧怕Nokia和iPhone
- 央视为什么要剑指谷歌
- 浅谈BOM