当前位置:编程学习 > JAVA >>

Java枚举:覆写toString,再看equals、hashCode

仍然采用 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 get

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,