Java 枚举:理解枚举本质

Java枚举是什么?关于Java枚举的一些理解 概述 Java枚举,即enum类型(Enumerate),是JDK1.5的新特性,其加入目的是为了更好的组织常量、增加可读性,使得项目结构更加清晰。 那么,它究竟是如何工作的呢?该如何更好的理解它呢? 且看下文 阅读详情

http://blog.csdn.net/veryitman/article/details/7945020

C 语言中可以这样来定义枚举

  1. enum color {  
  2.         RED=0, GREEN, BLUE, YELLOW  
  3. } col;  

关键字 enum 定义枚举,在定义枚举的同时,声明该枚举变量 col.


注意:C 语言中枚举成员的值是根据上下文自动加 1 的(GREEN = 1, BLUE = 2 等)。


C 语言中 switch 语句支持枚举类型

  1. #include<stdio.h>  
  2. int main() {  
  3.     enum color {  
  4.         RED=0, GREEN, BLUE, YELLOW  
  5.     } col;  
  6.   
  7.     int cl;  
  8.   
  9.     printf("0=red,1=green,2=blue,3=yellow. seclect:\n");  
  10.     scanf("%d",&cl);  
  11.   
  12.     col=(enum color) cl;  
  13.   
  14.     switch(col) {  
  15.         case RED:  
  16.             printf("the color is red\n");  
  17.             break;  
  18.         case GREEN:  
  19.             printf("the color is green\n");  
  20.             break;  
  21.         case BLUE:  
  22.              printf("the color is blue\n");  
  23.             break;  
  24.         case YELLOW:  
  25.             printf("the color is yellow\n");  
  26.             break;  
  27.         defalut:  
  28.             printf("no this color\n");  
  29.             break;  
  30.     }  
  31.   
  32.     return 0;  
  33. }  

那么,Java 里面的枚举与其类似,但是又不是完全一样。


Java 语言中定义枚举也是使用关键字 enum

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.     RED, GREEN, BLUE, YELLOW;  
  3. }  

上述定义了一个枚举类型 Color(可以说是类,编译之后是 Color.class).


上面的定义,还可以改成下面的这种形式

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.     RED(), GREEN(), BLUE(), YELLOW();  
  3. }  

到这里你就会觉得迷茫(如果你是初学者的话),为什么这样子也可以?


其实,枚举的成员就是枚举对象,只不过他们是静态常量而已。


使用 javap 命令(javap 文件名<没有后缀.class>)可以反编译 class 文件,如下:



我们可以使用普通类来模拟枚举,下面定义一个 Color 类。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class Color {  
  2.     private static final Color RED = new Color();  
  3.     private static final Color GREEN = new Color();  
  4.     private static final Color BLUE = new Color();  
  5.     private static final Color YELLOW = new Color();  
  6. }  

对比一下,你就明白了。


如果按照这个逻辑,是否还可以为其添加另外的构造方法?答案是肯定的!

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.         RED("red color", 0), GREEN("green color", 1),   
  3.         BLUE("blue color", 2), YELLOW("yellow color", 3);  
  4.   
  5.         Color(String name, int id) {  
  6.             _name = name;  
  7.             _id = id;  
  8.         }  
  9.   
  10.         String _name;  
  11.         int _id;  
  12. }  

为 Color 声明了两个成员变量,并为其构造带参数的构造器。

如果你这样创建一个枚举

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.         RED("red color", 0), GREEN("green color", 1),   
  3.         BLUE("blue color", 2), YELLOW("yellow color", 3);  
  4. }  

编译器就会报错

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. The constructor EnumDemo.Color(String, int) is undefined  

到此,你就可以明白,枚举和普通类基本一致(但是不完全一样)。


对于类来讲,最好将其成员变量私有化,然后,为成员变量提供 get、set 方法。

按照这个原则,可以进一步写好 enum Color.

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.         RED("red color", 0), GREEN("green color", 1),  
  3.         BLUE("blue color", 2), YELLOW("yellow color", 3);  
  4.   
  5.         Color(String name, int id) {  
  6.             _name = name;  
  7.             _id = id;  
  8.         }  
  9.   
  10.         private String _name;  
  11.         private int _id;  
  12.           
  13.         public void setName(String name) {  
  14.             _name = name;  
  15.         }  
  16.           
  17.         public void setId(int id) {  
  18.             _id = id;  
  19.         }  
  20.   
  21.         public String getName() {  
  22.             return _name;  
  23.         }  
  24.   
  25.         public int getId() {  
  26.             return _id;  
  27.         }  
  28. }  


但是,java 设计 enum 的目的是提供一组常量,方便用户设计。

如果我们冒然的提供 set 方法(外界可以改变其成员属性),好像是有点违背了设计的初衷。

那么,我们应该舍弃 set 方法,保留 get 方法。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public enum Color {  
  2.         RED("red color", 0), GREEN("green color", 1),  
  3.         BLUE("blue color", 2), YELLOW("yellow color", 3);  
  4.   
  5.         Color(String name, int id) {  
  6.             _name = name;  
  7.             _id = id;  
  8.         }  
  9.   
  10.         private String _name;  
  11.         private int _id;  
  12.           
  13.         public String getName() {  
  14.             return _name;  
  15.         }  
  16.   
  17.         public int getId() {  
  18.             return _id;  
  19.         }  
  20. }  

普通类,我们可以将其实例化,那么,能否实例化枚举呢?


在回答这个问题之前,先来看看,编译之后的 Color.class 文件

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public static enum Color {  
  2.         RED("red color", 0), GREEN("green color", 1),  
  3.         BLUE("blue color", 2), YELLOW("yellow color", 3);  
  4.   
  5.         private String _name;  
  6.         private int _id;  
  7.   
  8.         private Color(String name, int id) {  
  9.             this._name = name;  
  10.             this._id = id;  
  11.         }  
  12.   
  13.         public String getName() {  
  14.             return this._name;  
  15.         }  
  16.   
  17.         public int getId() {  
  18.             return this._id;  
  19.         }  
  20. }  

可以看出,编译器淘气的为其构造方法加上了 private,那么也就是说,我们无法实例化枚举。


所有枚举类都继承了 Enum 类的方法,包括 toString 、equals、hashcode 等方法。

因为 equals、hashcode 方法是 final 的,所以不可以被枚举重写(只可以继承)。

但是,可以重写 toString 方法。


关于 Enum 源码,详见附录!


那么,使用 Java 的不同类来模拟一下枚举,大概是这个样子

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package mark.demo;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. public class Color {  
  7.     private static final Color RED = new Color("red color", 0);  
  8.     private static final Color GREEN = new Color("green color", 1);  
  9.     private static final Color BLUE = new Color("blue color", 2);  
  10.     private static final Color YELLOW = new Color("yellow color", 3);  
  11.   
  12.     private final String _name;  
  13.     private final int _id;  
  14.   
  15.     private Color(String name, int id) {  
  16.         _name = name;  
  17.         _id = id;  
  18.     }  
  19.   
  20.     public String getName() {  
  21.         return _name;  
  22.     }  
  23.   
  24.     public int getId() {  
  25.         return _id;  
  26.     }  
  27.   
  28.     public static List<Color> values() {  
  29.         List<Color> list = new ArrayList<Color>();  
  30.         list.add(RED);  
  31.         list.add(GREEN);  
  32.         list.add(BLUE);  
  33.         list.add(YELLOW);  
  34.         return list;  
  35.     }  
  36.   
  37.     @Override  
  38.     public String toString() {  
  39.         return "the color _name=" + _name + ", _id=" + _id;  
  40.     }  
  41.   
  42. }  


附录


Enum.java

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * %W% %E% 
  3.  * 
  4.  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. 
  5.  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
  6.  */  
  7.   
  8. package java.lang;  
  9.   
  10. import java.io.Serializable;  
  11. import java.io.IOException;  
  12. import java.io.InvalidObjectException;  
  13. import java.io.ObjectInputStream;  
  14. import java.io.ObjectStreamException;  
  15.   
  16. /** 
  17.  * This is the common base class of all Java language enumeration types. 
  18.  * 
  19.  * @author  Josh Bloch 
  20.  * @author  Neal Gafter 
  21.  * @version %I%, %G% 
  22.  * @since   1.5 
  23.  */  
  24. public abstract class Enum<E extends Enum<E>>  
  25.         implements Comparable<E>, Serializable {  
  26.     /** 
  27.      * The name of this enum constant, as declared in the enum declaration. 
  28.      * Most programmers should use the {@link #toString} method rather than 
  29.      * accessing this field. 
  30.      */  
  31.     private final String name;  
  32.   
  33.     /** 
  34.      * Returns the name of this enum constant, exactly as declared in its 
  35.      * enum declaration. 
  36.      *  
  37.      * <b>Most programmers should use the {@link #toString} method in 
  38.      * preference to this one, as the toString method may return 
  39.      * a more user-friendly name.</b>  This method is designed primarily for 
  40.      * use in specialized situations where correctness depends on getting the 
  41.      * exact name, which will not vary from release to release. 
  42.      * 
  43.      * @return the name of this enum constant 
  44.      */  
  45.     public final String name() {  
  46.     return name;  
  47.     }  
  48.   
  49.     /** 
  50.      * The ordinal of this enumeration constant (its position 
  51.      * in the enum declaration, where the initial constant is assigned 
  52.      * an ordinal of zero). 
  53.      *  
  54.      * Most programmers will have no use for this field.  It is designed 
  55.      * for use by sophisticated enum-based data structures, such as 
  56.      * {@link java.util.EnumSet} and {@link java.util.EnumMap}. 
  57.      */  
  58.     private final int ordinal;  
  59.   
  60.     /** 
  61.      * Returns the ordinal of this enumeration constant (its position 
  62.      * in its enum declaration, where the initial constant is assigned 
  63.      * an ordinal of zero). 
  64.      *  
  65.      * Most programmers will have no use for this method.  It is 
  66.      * designed for use by sophisticated enum-based data structures, such 
  67.      * as {@link java.util.EnumSet} and {@link java.util.EnumMap}. 
  68.      * 
  69.      * @return the ordinal of this enumeration constant 
  70.      */  
  71.     public final int ordinal() {  
  72.     return ordinal;  
  73.     }  
  74.   
  75.     /** 
  76.      * Sole constructor.  Programmers cannot invoke this constructor. 
  77.      * It is for use by code emitted by the compiler in response to 
  78.      * enum type declarations. 
  79.      * 
  80.      * @param name - The name of this enum constant, which is the identifier 
  81.      *               used to declare it. 
  82.      * @param ordinal - The ordinal of this enumeration constant (its position 
  83.      *         in the enum declaration, where the initial constant is assigned 
  84.      *         an ordinal of zero). 
  85.      */  
  86.     protected Enum(String name, int ordinal) {  
  87.     this.name = name;  
  88.     this.ordinal = ordinal;  
  89.     }  
  90.   
  91.     /** 
  92.      * Returns the name of this enum constant, as contained in the 
  93.      * declaration.  This method may be overridden, though it typically 
  94.      * isn't necessary or desirable.  An enum type should override this 
  95.      * method when a more "programmer-friendly" string form exists. 
  96.      * 
  97.      * @return the name of this enum constant 
  98.      */  
  99.     public String toString() {  
  100.     return name;  
  101.     }  
  102.   
  103.     /** 
  104.      * Returns true if the specified object is equal to this 
  105.      * enum constant. 
  106.      * 
  107.      * @param other the object to be compared for equality with this object. 
  108.      * @return  true if the specified object is equal to this 
  109.      *          enum constant. 
  110.      */  
  111.     public final boolean equals(Object other) {   
  112.         return this==other;  
  113.     }  
  114.   
  115.     /** 
  116.      * Returns a hash code for this enum constant. 
  117.      * 
  118.      * @return a hash code for this enum constant. 
  119.      */  
  120.     public final int hashCode() {  
  121.         return super.hashCode();  
  122.     }  
  123.   
  124.     /** 
  125.      * Throws CloneNotSupportedException.  This guarantees that enums 
  126.      * are never cloned, which is necessary to preserve their "singleton" 
  127.      * status. 
  128.      * 
  129.      * @return (never returns) 
  130.      */  
  131.     protected final Object clone() throws CloneNotSupportedException {  
  132.     throw new CloneNotSupportedException();  
  133.     }  
  134.   
  135.     /** 
  136.      * Compares this enum with the specified object for order.  Returns a 
  137.      * negative integer, zero, or a positive integer as this object is less 
  138.      * than, equal to, or greater than the specified object. 
  139.      *  
  140.      * Enum constants are only comparable to other enum constants of the 
  141.      * same enum type.  The natural order implemented by this 
  142.      * method is the order in which the constants are declared. 
  143.      */  
  144.     public final int compareTo(E o) {  
  145.     Enum other = (Enum)o;  
  146.     Enum self = this;  
  147.     if (self.getClass() != other.getClass() && // optimization  
  148.             self.getDeclaringClass() != other.getDeclaringClass())  
  149.         throw new ClassCastException();  
  150.     return self.ordinal - other.ordinal;  
  151.     }  
  152.   
  153.     /** 
  154.      * Returns the Class object corresponding to this enum constant's 
  155.      * enum type.  Two enum constants e1 and  e2 are of the 
  156.      * same enum type if and only if 
  157.      *   e1.getDeclaringClass() == e2.getDeclaringClass(). 
  158.      * (The value returned by this method may differ from the one returned 
  159.      * by the {@link Object#getClass} method for enum constants with 
  160.      * constant-specific class bodies.) 
  161.      * 
  162.      * @return the Class object corresponding to this enum constant's 
  163.      *     enum type 
  164.      */  
  165.     public final Class<E> getDeclaringClass() {  
  166.     Class clazz = getClass();  
  167.     Class zuper = clazz.getSuperclass();  
  168.     return (zuper == Enum.class) ? clazz : zuper;  
  169.     }  
  170.   
  171.     /** 
  172.      * Returns the enum constant of the specified enum type with the 
  173.      * specified name.  The name must match exactly an identifier used 
  174.      * to declare an enum constant in this type.  (Extraneous whitespace 
  175.      * characters are not permitted.)  
  176.      * 
  177.      * @param enumType the <tt>Class</tt> object of the enum type from which 
  178.      *      to return a constant 
  179.      * @param name the name of the constant to return 
  180.      * @return the enum constant of the specified enum type with the 
  181.      *      specified name 
  182.      * @throws IllegalArgumentException if the specified enum type has 
  183.      *         no constant with the specified name, or the specified 
  184.      *         class object does not represent an enum type 
  185.      * @throws NullPointerException if <tt>enumType</tt> or <tt>name</tt> 
  186.      *         is null 
  187.      * @since 1.5 
  188.      */  
  189.     public static <T extends Enum<T>> T valueOf(Class<T> enumType,  
  190.                                                 String name) {  
  191.         T result = enumType.enumConstantDirectory().get(name);  
  192.         if (result != null)  
  193.             return result;  
  194.         if (name == null)  
  195.             throw new NullPointerException("Name is null");  
  196.         throw new IllegalArgumentException(  
  197.             "No enum const " + enumType +"." + name);  
  198.     }  
  199.   
  200.     /** 
  201.       * prevent default deserialization 
  202.       */  
  203.     private void readObject(ObjectInputStream in) throws IOException,  
  204.         ClassNotFoundException {  
  205.             throw new InvalidObjectException("can't deserialize enum");  
  206.     }  
  207.   
  208.     private void readObjectNoData() throws ObjectStreamException {  
  209.         throw new InvalidObjectException("can't deserialize enum");  
  210.     }  
  211.   
  212.     /** 
  213.      * enum classes cannot have finalize methods. 
  214.      */  
  215.     protected final void finalize() { }  
  216. }  








版权声明:本文为博主原创文章,未经博主允许不得转载。

JAVA的枚举 本例和上例的执行结果完全相同。 阅读详情

相关推荐

DANN实战:用Python手把手教你实现对抗迁移学习(附完整代码)

本文通过Python实战教程,手把手教你实现DANN对抗迁移学习模型,以解决领域偏移问题。文章详细解析了领域对抗神经网络的核心机制,包括梯度反转层和领域判别器的构建,并提供了从数据准备、模型搭建到训练评估的完整代码,帮助开发者快速掌握这一强大的无监督领域自适应技术。

net55的博客 321

MyBatis对于Java对象里的枚举类型处理

本文转载自:http://www.shuyangyang.com.cn/jishuliangongfang/Javabiancheng/2015-01-26/229.html  平时咱们写程序实体类内或多或少都会有枚举类型属性,方便嘛。但是mybatis里怎么处理他们的增删改查呢? 要求: 插入的时候,会用枚举的定义插入数据库,我们希望在数据库中看到的是数字或者其他东西; 查询的时

suchaochen的博客 944

CORS(跨域资源共享)漏洞解决方法

最近,测试环境上的项目被360测试人员检测出来有一个CORS漏洞,以下记录下漏洞问题与解决方法。 一、低危漏洞:CORS漏洞问题 测试人员访问某个url,将请求头中的Origin字段修改为任意值,结果仍然能获得正确的响应报文,就说明有CORS漏洞。 当CORS的设置不正确时,就会带来安全问题;当响应头中的Access-Control-Allow-Origin设置为null或*时,表示信任任何域,这时候就可能引入安全问题。 修复方法是合理配置CORS,判断Origin是否合法;具体说就是不让在nginx或t

BHSZZY的博客 3万+

Package 'javafx.stage' is declared in module 'javafx.graphics', which is not in the module graph

使用itellij idea写javafx程序时,刚开始挺顺利的,功能也实现了。后来过了一段时间重新打开idea看项目时忽然发现一大片红色错误提示。我以为是少jar包导致的,就导入jar包,专门创建了一个maven工程重构项目来解决jar包问题,然而并没有说什么卵用,还是一大片红色。把光标放到红色代码上就提示如标题所示的错误信息。也是很郁闷。 直接运行一下项目,发现虽然报错,但是项目能正常运行,...

never_give_up_666的博客 4597

RT-Thread新手必看:FMT飞控移植中的5个常见坑及解决方案

本文针对RT-Thread新手在移植FMT开源飞控时遇到的5大核心挑战,提供了深度解决方案。内容涵盖SCons构建系统配置、RT-Thread版本API兼容性处理、BSP与设备驱动适配、TOML系统配置解析以及算法模块集成调试,帮助开发者高效避坑,成功完成飞控移植。

gin88的博客 258

LIN 协议测试

如下图,使用PicoScope示波器抓取报文Id为0x18的波形,数据场Byte1的长度为519.994μs,Byte1共10bit,计算位时间:tBit= 519.994/10 = 51.9994μs,符合预期。■ 唤醒信号是一个250μs到5ms的显性电平,发送唤醒信号的节点发送唤醒信号后150ms~250ms没有收到其他节点的唤醒信号,发送唤醒信号的节点发送新的唤醒信号。对于LIN主节点,检查发送的帧头长度是否符合设计要求,对于LIN从节点,检查从节点能否正确响应设计范围之内的帧头长度。

m0_63902994的博客 5957

【IDEA】报错:Package ‘java.lang‘ is declared in module ‘java.base‘, which is not in the module graph

Package 'java.lang' is declared in module 'java.base', which is not in the module graph报错解决

Aprilila_的博客 8877

实现自己的AI视频监控系统-第一章-视频拉流与解码4(重点)

使用本章节代码请引用此CSDN链接,尊重知识、尊重他人劳动。这一小节将正式进入代码实现部分。我们会结合实际业务需要和拉流解码流程实现一个灵活、轻便、易用的视频拉流解码模块,支持多路视频拉流解码且可以动态更新更新内部参数。在开始设计之初,我们首先考虑的是使用大家所熟知的 **OpenCV(cv2.VideoCapture)**来实现拉流和解码功能。它的 API 简单直接,几行代码就能实现从网络流中读取帧,对于快速原型验证来说非常方便。然而,在经过深入的调研和测试后,我们发现 OpenCV 在处理多路视频流、追

soulproficiency的博客 2278

MyBatis 查询映射自定义枚举

背景                MyBatis查询若想映射枚举类型,则需要从 EnumTypeHandler 或者 EnumOrdinalTypeHandler 中选一个来使用            1. EnumOrdinalTypeHandler 是按照序列来存取的,对应数据库的设计为 使用0开始按升序。            2. EnumTypeHandler 是按照枚举的名字来存取的

jerome 1万+

用 DCL 编写 DEFINE ROLE:把 ABAP CDS 的访问控制做对、做实、做漂亮

摘要: 本文详细讲解如何在ABAP CDS中使用DCL(数据控制语言)实现行级授权,确保业务数据安全。核心要点包括: DCL原理:通过DEFINE ROLE将访问条件绑定到CDS实体,自动附加到查询条件(类似隐式WHERE),保障授权一致性。 关键语法: 必需注解@MappingRole: true声明映射角色。 ASPECT pfcg_auth关联PFCG权限对象,动态控制访问。 注解@AccessControl.authorizationCheck定义授权检查策略(如#CHECK启用强制控制)。 优先级

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 169

x265开源编码器源码框架深度解析

x265开源编码器源码框架深度解析

世界那么大,我想去看看~【随心更新,看世界去了😄】 689

java枚举 不推荐_你可能不了解的java枚举

枚举在java里也算个老生长谈的内容了,每当遇到一组需要类举的数据时我们都会自然而然地使用枚举类型:public enum Color {RED, GREEN, BLUE, YELLOW;public static void main(String[] args) {Color red = Color.RED;Color redAnother = Color.RED;Color blue = Co...

weixin_42522964的博客 345

要理解成本中心与利润中心的根本区别与适用场景,需从 “责任范围、考核核心、管理目标” 三个核心维度切入,同时明确二者在企业管理体系中的联动关系

摘要: 成本中心与利润核心区别在于责任范围、考核目标和管理权限。成本中心仅对成本负责(如生产、行政部门),考核成本控制能力;利润中心需同时管控成本与收入(如独立产品线、区域分公司),以利润为核心指标。选择标准取决于单元是否拥有收入决策权:无收入权的支持/执行部门设为成本中心,能独立经营、核算盈亏的单元设为利润中心。二者协同运作,成本中心为利润中心提供基础支持,利润中心的需求动态影响成本中心资源配置,共同实现企业整体目标。划分本质是确保考核指标与责任范围匹配,避免管理失效。

金牌架构师 671

enum枚举类,java中枚举类的概念,什么是枚举类enum,结合实战案例理解

enum、java

zhangfuping123456789的博客 1万+

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

深入理解 Java 枚举

???? 本文已归档到:「javacore」 ???? 本文中的示例代码已归档到:「javacore」 1. 简介 enum 的全称为 enumeration, 是 JDK5 中引入的特性。 在 Java 中,被 enum 关键字修饰的类型就是枚举类型。形式如下: enum ColorEn { RED, GREEN, BLUE } 枚举的好处:可以将常量组织起来,统一进行管理。 枚举的典型应用场景:...

静默虚空的专栏 999

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

初探Java枚举类

目录1.何为枚举2.枚举本质3.枚举特性3.1基本特性3.2枚举方法3.3 枚举接口3.4 获取枚举元素3.5 状态机4.枚举工具类4.1EnumSet4.2EnumMap5.总结 1.何为枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景。如性别(男、女),颜色(红、黄、蓝),星期(星期一、星期二…星期日)均可以使用枚举存储。 2.枚举本质 从源码角度: public abstract class Enum<E extends Enum

增量星球 1712

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

对于Java枚举的理解

一,Java枚举其实本质是一种特殊的类 特殊在于:对于一般的类在定义时是不确定实际使用的时候到底会创建多少个对象,可以具需求不定量的创建其类对象。而枚举是在类定义的时候可以确定对象个数的( 也就是说可以一一列举出来的,固定的数个对象 )。 二,通过普通的类描述,简单模拟枚举的特点,从而理解枚举 1.枚举的特点是对象个数是确定的,所以首先我们需要确保构造方法私有,才能确定对象不能随意被创建。 2.再...

ONEMORE6499的博客 319

java enum本质_枚举的本质

在Java面试中,枚举是一个绕不开的话题。通过对枚举的考察,面试官可以快速判断求职者对Java基础知识的掌握情况。今天,我们就来聊一聊这个小而美的类:枚举。你是否被问到过以下的问题:1.枚举允许继承类吗?2.枚举允许实现接口吗?3.枚举可以用等号比较吗?4.可以继承枚举吗?5.枚举可以实现单例模式吗?6. 当使用compareTo()比较枚举时,比较的是什么?7. 当使用equals()比较枚举的...

weixin_33954874的博客 213

java——枚举

本例和上例的执行结果完全相同。

zjx1415926的博客 1319

Java枚举源码级理解

title: Java枚举源码级理解 date: 2020-11-08 16:47:39 tags: [Java枚举, 源码理解, Java源码理解, 枚举, JavaBase, Java基础] categories: Java Java中枚举体的一些东西很怪,看了源码之后恍然大悟,收获颇丰 废话不多说,直接上代码 第一种形式 枚举体定义 public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;.

qq_27868521的博客 277

Java 枚举与泛型

Java 枚举 之前写过的文档:https://blog.csdn.net/Roobert_Chao/article/details/78637972

RobertChao的博客 5122

Java枚举深入理解以及HttpStatus类的使用

1枚举 1.1枚举基础 由于每个程序员在开始编程时一般会首先学习C/C++,然后再学习Java,在C++中枚举是可以定义枚举值为特定的整数的,但这在Java中却没有得到支持,大概C/C++中的枚举代表了数值型常量多一些,而Java中枚举值,更确切的说为枚举类型对象。在Java中定义一个简单的枚举类如下所示: public enum Season { SPRING, SUMMER, AUTU...

不当初 7990
上一篇: 浅析Java中的final关键字
下一篇: Java注释@interface的用法【转】
nickshang
博客等级 码龄20年 0粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值