Java枚举:覆写toString,再看equals、hashCode
来源:互联网 发布:程序员的数学 epub 编辑:程序博客网 时间:2024/05/21 16:56
仍然采用 Java 枚举:理解枚举本质 例子。稍加修改。
[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 方法:
[java]
/**
* 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() {
turn name;
}注释写的很明白,返回的是 the name of enum constant(枚举常量值的名字)。
如果我们想要得到如期的结果,需要重写 toString 方法。
[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;
}
@Override
public String toString() {
return _name + "," + _id;
}
}
}上面代码里面使用增强 for 循环调用 values() 方法,遍历枚举的值,也可以这样来遍历:
[java]
for (Color color : Color.values()) {
System.out.println(color.getName() + "," + color.getId());
}
Enum 类的 equals、hashCode 方法
[java]
/**
* 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 效果。
[java]
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;
}
}
测试结果:[java]
public static void main(String[] args) {
System.out.println(Color.RED.equals(""));// false
System.out.println(Color.RED.equals("red color"));// false
System.out.println(Color.RED.equals("RED"));// false
System.out.println(Color.RED.equals(Color.BLUE));// false
// 同一个对象
System.out.println(Color.RED.equals(Color.RED));// true
// 实际是两个 String
System.out.println(Color.RED.toString().equals("RED"));// true
// 实际是两个 String
System.out.println(Color.RED.getName().equals("red color"));// true
// hashCode
System.out.println("Color.RED.hashCode = " + Color.RED.hashCode());
}如果重写了类似上面的 toString 方法,那么
[java] view plaincopyprint?
System.out.println(Color.RED.toString().equals("RED"));
返回的可能是 true,也可能是 false,这取决于你的 toString 具体实现。
如下面这种实现方法:返回 _name 的大写形式。
[java]
@Override
public String toString() {
return this._name.toUpperCase();
}综上,建议根据需要可以重写 toString 方法,善用 equals 方法。
转载自:http://www.2cto.com/kf/201209/153314.htm
l
- 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 方法重写
- 编写你线程的主体入口点
- 蓝牙网络的结构
- ArtDialog V6的简单使用
- Android SDK更新失败的解决方法
- 学习笔记:构造函数转发
- Java枚举:覆写toString,再看equals、hashCode
- Java构造和解析Json数据的两种方法详解二
- BlockingQueue
- freemarker学习
- Eclipse + NDK开发 问题集
- 打通B/S与C/S !让HTML5 WebSocket与.NET Socket公用同一个服务端!
- 在工作中如何做到优秀,在工作之外如何做到自己(一)
- 阿斯达四大四大
- 将数字转换为汉字