zoukankan      html  css  js  c++  java
  • java枚举类型

    一:

    有时候变量的取值是在有限的集合内,比如说服务器状态:上线、下线、报废等。

    可以将这些装备标记为:1、2、3 但是这种情况,我们在给变量赋值的有时候赋值为错误的值比如:0 或者true

    这时候就出现枚举数据类型,枚举包含有限个命名:

    public enum Size { 上线,下线,报废 }

    对于枚举在文档中解释:http://www.xyzws.com/Javafaq/what-can-or-can-not-for-the-java-enum-type-when-you-use-it/136

    An enum is a special kind of class. The Java Language Specifcation said "There are two kinds of class declarations - normal class declarations and enum declarations". An enum declaration specifies a new named enum type. The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type. An enum typehas no instances other than those defined by its enum constants (8.9 Enums in Java Language Specification).

    • An enum can be declared outside or inside a class, but can not be declared in a method. Only public or default modifier can be used by the declaration of a top-level enum. You can use publicdefaultprotected or private modifier on the declaration of a nest enum.
    • All of enum type are subclasses of java.Lang.Enum, since Java does not support multiple inheritance, an enum cannot extend anything else.
      • The constructor of java.Lang.Enum is protected.
      • The enum variables are join serialazation because java.Lang.Enum implements Serializable and Comparable interfaces.
      • The java.Lang.Enum provides final methods (see Enum API Doc): name, compareTO, equals, hashCode, ordinal, clone, and getDeclaringClass. The final clonemethod in Enum ensures that enum constants can never be cloned. An enum typethat contains constant-specific class bodies cannot override final enum methods.
      • You can overide toString() method in . The default toString() returns the name of this enum constant, as contained in its enum declaration. An enum type should override this method when a more "programmer-friendly" string form exists.

    大致意思:

    enum是一种特殊java类,在java语言中我们常说,在java类中有2种类声明,一种标准声明,一种就是enum声明。而enum声明是一个新的enum类型,在声明enum类型的时候,构造器中,包含多个java常量。一个常量被定义为enum类型的一个实例。一个enum类型只有传入常量为enum实例。其实声明一个enum类型对象也是声明一个类。

    一个enum对象只能在一个类的外部或者内部声明,不能在方法内声明,这一点需要注意。只能用public或者默认修饰符(默认为protected)来声明最顶层的enum,你可以使用public、protected、private声明嵌套内部的enum。

    enum类是java.Lang.Enum的子类。

    Enum类声明一些final方法。

    有可以覆盖toString()方法,默认toString()返回enum对象的字段常量。在声明enum类型对象的时候。

    在声明statu枚举的时候,该Statu也是类,所以在遍历的时候,以此为类型。

    1 public class M_J {
    2     enum Statu{上线,下线};
    3     public static void  main(String ... args){
    4           for (Statu n:Statu.values()){
    5               System.out.println(n);
    6           }
    7 
    8     }
    9 }

    常用的方法:values()返回枚举的对应的字符串常量数组。

    toString()返回单个枚举常量。

     1 package com.company;
     2 
     3 
     4 
     5 public class M_J {
     6     public enum  new_mj {上线,下线};
     7     public static  void main(String ... args){
     8         System.out.println(new_mj.上线.toString());//返回枚举常量名字。
     9         System.out.println(new_mj.valueOf("上线").toString());//返回枚举常量valueof
    10         new_mj  s=  Enum.valueOf(new_mj.class,"上线");//返回枚举常量valueof 是静态方法。
    11         System.out.println(Enum.valueOf(new_mj.class,"上线").ordinal());//获取对应常量的枚举位置。
    12         System.out.println(s.toString());
    13         new_mj[] new_m=new_mj.values();//转换成对应常量数组。
    14         for (new_mj i:new_m){System.out.println(i);}
    15 
    16     }
    17 }

     

  • 相关阅读:
    存储过程使用:
    java map,set,list
    Jbox帮助文档,默认的属性含义
    checkboxlist的说明及使用
    java中两种select方式,,一种从数据表中读取
    IE6下<a href="#">与<a href="javascript:void(0);">的区别
    用javascript如何在框架间传值
    a href=#与 a href=javascript:void(0) 的区别
    关于如何导入GPUImage
    GPUImage实现过程
  • 原文地址:https://www.cnblogs.com/evilliu/p/7681445.html
Copyright © 2011-2022 走看看