1. Programs that use the int enum pattern are brittle. Because int enums are compile-time constants, they are compiled into the clients that use them. If the int associated with an enum constant is changed, its clients must be recompiled. If they aren’t, they will still run, but their behavior will be undefined.
2. The basic idea behind Java’s enum types is simple: they are classes that export one instance for each enumeration constant via a public static final field. Enum types are effectively final, by virtue of having no accessible constructors.
3. enum types let you add arbitrary methods and fields and implement arbitrary interfaces. They provide high-quality implementations of all the Object methods, they implement Comparable and Serializable, and their serialized form is designed to withstand most changes to the enum type.
4. There is a better way to associate a different behavior with each enum constant: declare an abstract method in the enum type, and override it with a concrete method for each constant in a constant-specific class body. Such methods are knows as constant-specific method implementations:
// Enum type with constant-specific method implementations public enum Operation { PLUS { double apply(double x, double y){return x + y;} }, MINUS { double apply(double x, double y){return x - y;} }, TIMES { double apply(double x, double y){return x * y;} }, DIVIDE { double apply(double x, double y){return x / y;} }; abstract double apply(double x, double y); }
Abstract methods in an enum type must be overridden with concrete methods in all of its constants.
5. Enum types have an automatically generated valueOf(String) method that translates a constant’s name into the constant itself. If you override the toString method in an enum type, consider writing a fromString method to translate the custom string representation back to the corresponding enum.
6. Enum constructors aren’t permitted to access the enum’s static fields, except for compile-time constant fields. This restriction is necessary because these static fields have not yet been initialized when the constructors run.
7. Consider the strategy enum pattern if multiple enum constants share common behaviors:
// The strategy enum pattern enum PayrollDay { MONDAY(PayType.WEEKDAY), TUESDAY(PayType.WEEKDAY), WEDNESDAY(PayType.WEEKDAY), THURSDAY(PayType.WEEKDAY), FRIDAY(PayType.WEEKDAY), SATURDAY(PayType.WEEKEND), SUNDAY(PayType.WEEKEND); private final PayType payType; PayrollDay(PayType payType) { this.payType = payType; } double pay(double hoursWorked, double payRate) { return payType.pay(hoursWorked, payRate); } // The strategy enum type private enum PayType { WEEKDAY { double overtimePay(double hours, double payRate) { return hours <= HOURS_PER_SHIFT ? 0 : (hours - HOURS_PER_SHIFT) * payRate / 2; } }, WEEKEND { double overtimePay(double hours, double payRate) { return hours * payRate / 2; } }; private static final int HOURS_PER_SHIFT = 8; abstract double overtimePay(double hrs, double payRate); double pay(double hoursWorked, double payRate) { double basePay = hoursWorked * payRate; return basePay + overtimePay(hoursWorked, payRate); } } }
相关推荐
Item 34: Use enums instead of int constants Item 35: Use instance fields instead of ordinals Item 36: Use EnumSet instead of bit fields Item 37: Use EnumMap instead of ordinal indexing Item 38: ...
10. **条目10:优先考虑使用枚举而非int常量(Use Enums Instead of Int Constants)** 枚举提供了类型安全,可以方便地添加方法和实现接口,比使用整数常量更易管理和维护。 11. **条目11:使用泛型(Generics)*...
All of a project's header files should be listed as descentants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For...
Views And Iterators Instead Of Lists Ordering Comparisons Integers Text Vs. Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already...
学生信息管理系统-----------无数据库版本。资源来源于网络分享,如有侵权请告知!
2024年福建省村级(居委会)行政区划shp数据集 坐标系:WGS1984
win32汇编环境,对话框中显示bmp图像文件
基于STM8单片机的编程实例,可供参考学习使用,希望对你有所帮助
电动汽车动力系统匹配计算模型:输入整车参数及性能要求,一键生成驱动系统的扭矩功率峰值转速等参数。 2、整车动力经济性计算模型:包含NEDC WLTC CLTC工况,输入整车参数可生成工况电耗、百公里电耗、匀速工况续航、百公里电耗等信息。 实际项目中使用的计算仿真模型.
2020CCF下降2020 CCF大数据与计算智能大赛-非结构化商业文本信息中隐私信息识别-第7名方案bert base + flat + crf + fgm + swa + pu learning策略 + clue数据集 = test1单模0.906词向量https://github.com/Embedding/Chinese-Word-Vectors SGNS(Mixed-large 综合)loss mask相关代码为pu learning策略的实现主要模块版本 python 3.6.9火炬 1.1.0变压器 3.0.2pytorchcrf 1.2.0torchcontrib 0.0.2
计算机系毕业设计
基于STM8单片机的编程实例,可供参考学习使用,希望对你有所帮助
基于SpringBoot+MySQL图书销售管理系统网上书店项目源码+数据库(高分毕业设计&课程设计) 该项目是个人大作业项目源码,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!!!评审分达到95分以上。资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 图书管理系统 框架介绍 依赖 版本 Spring Boot 2 Mybatis Plus 3.5.3 jjwt 0.11.2 vue 2.0 ehcache 2.10.9 系统采用前后端分离,前端打包后放在 /resources/static 目录下面 直接启动后端工程即可访问 系统亮点 采用rsa非对称加密算法生成 jwt认证密钥 springboot集成ehcache作为缓存 采用aop方式记录接口访问日志 使用h2内存数据库,启动应用执行自动建表语句和初始化数据 统一异常和响应进行封装 集成springdoc作为接口文档 系统访问 http://wholevoid.com:9090/ 用户名及密码 admin/123456 图书管理系统 框
二极管钳位三电平VSG仿真模型 1.加入中点电位平衡 2.仿真有视频教程 3.THD均<5% 可以在此模型的基础上加入自适应控制、模型预测控制等等
基于Halcon的机器视觉试验平台的设计与研究 20181126
腾讯云开发者工具套件(SDK)3.0,SDK3.0是云 API3.0 平台的配套工具。目前已经支持cvm、vpc、cbs等产品,后续所有的云服务产品都会接入进来。新版SDK实现了统一化,具有各个语言版本的SDK使用方法相同,接口调用方式相同,统一的错误码和返回包格式这些优点。 为方便 Python 开发者调试和接入腾讯云产品 API,这里向您介绍适用于 Python 的腾讯云开发工具包,并提供首次使用开发工具包的简单示例。让您快速获取腾讯云 Python SDK 并开始调用。
说明文档1 队伍简介初赛名次第42名复赛名次第22名队伍名把球给我两名队员全部来自中国科学院大学2 算法思路首先手工标记第一阶段2015年和2017的图像里的建筑物,将大图像划分成小图像,训练多个模型,识别出图像中的建筑物,不对测试集(第二阶段的图像)进行任何标注,直接在图像上预测,分别识别出2015和2017的建筑物,再将所得的两张建筑物图像相减,对结果文件进行边缘平滑和散点去除即可得出最后的结果。切割成160*160、224*224、256*256大小的小图片训练模型基于第一阶段的训练数据,分别训练了deeplabv2、resnet_fcn两个模型,分别在3种大小的图像上训练得到了5个模型(由于resnet最小图像限制为197,只用了224和256两种大小的图像),设定输出概率大于0.5判定为建筑物,小于0.5则为非建筑物未在测试数据上进行建筑物标注,线下建筑物识别准确率82%左右,经过标注,建筑物识别准确率能达到90%。复赛初始提交,泛化成绩0.742。经过数据标注和再训练,最终成绩0.829。数据增强用于模型训练阶段,数据后处理是对
tdm64-gcc-5.1.0-2 (不盈利分享)