1. For the most part, extensibility of enums turns out to be a bad idea. It is confusing that elements of an extension type are instances of the base type and not vice versa. There is no good way to enumerate over all of the elements of a base type and its extension.
2. There is at least one compelling use case for extensible enumerated types, which is operation codes. An opcode is an enumerated type whose elements represent operations on some machine. Sometimes it is desirable to let the users of an API provide their own operations, effectively extending the set of operations provided by the API. The basic idea is to take advantage of the fact that enum types can implement arbitrary interfaces by defining an interface for the opcode type and an enum that is the standard implementation of the interface. You can define another enum type that implements this interface and use instances of this new type in place of the base type.
3. Not only is it possible to pass a single instance of an “extension enum” anywhere a “base enum” is expected; it is possible to pass in an entire extension enum type and use its elements in addition to or instead of those of the base type:
public static void main(String[] args) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); test(ExtendedOperation.class, x, y); } private static <T extends Enum<T> & Operation> void test( Class<T> opSet, double x, double y) { for (Operation op : opSet.getEnumConstants()) System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y)); }
A less complex and more flexible way is :
public static void main(String[] args) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); test(Arrays.asList(ExtendedOperation.values()), x, y); } private static void test(Collection<? extends Operation> opSet, double x, double y) { for (Operation op : opSet) System.out.printf("%f %s %f = %f%n", x, op, y, op.apply(x, y)); }
It allows the caller to combine operations from multiple implementation types.
相关推荐
Item 38: Emulate extensible enums with interfaces Item 39: Prefer annotations to naming patterns Item 40: Consistently use the Override annotation Item 41: Use marker interfaces to define types 7 ...
emulate_right_click 将 CONTROL+left_click 转换为 right_click 鼠标事件。 我不喜欢我的触摸板(触控板)上的双指点击,所以我尝试了这个。 如果您的鼠标没有右键——或者它坏了,这会很有用。 免责声明:这是我...
**RSLogix Emulate 500:深入了解与应用** RSLogix Emulate 500是一款由Rockwell Automation开发的软件工具,主要用于模拟和测试Allen Bradley PLC(可编程逻辑控制器)系列中的Ladder Logic 500程序。这款仿真软件...
资源分类:Python库 所属语言:Python 资源全名:iotile_emulate-0.5.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
教你一步步的如何使用Emulate5000
Emulate仿真模拟教材PPT学习教案 Emulate仿真模拟教材PPT学习教案是关于RSLogix Emulate 5000 Logix5000处理器模拟软件的学习教案。该教案主要介绍了Emulate 5000的基础知识和应用场景,并提供了详细的操作步骤和...
emulate5000使用录像,资源共享,共同学习进步!
RSLogix_Emulate500使用教程
【标题】:“emulate.c:具有几个基本说明的简单虚拟机” 这个标题指的是一个名为“emulate.c”的源代码文件,它实现了一个简单的虚拟机。虚拟机是一种软件模拟,能够执行特定指令集的程序,而这些指令集并不直接...
RSlogix Emulate 5000软件是Rockwell AB公司提供用来仿真运行PLC的程序,通过它可以在计算机上虚拟AB PLC的连接、烧写、运行操作,就像实际操作PLC一样。本文介绍RSlogix Emulate 5000软件的安装和使用方法。
标题中的"PyPI 官网下载 | iotile-emulate-0.5.1.tar.gz"揭示了我们正在处理的是一个在Python Package Index (PyPI) 上发布的软件包。PyPI是Python开发者发布自己编写的模块、库或工具的地方,方便其他用户安装和...
TAdvTabSet & TAdvPageControl v1.5----------- New: TabStyle tsDelphi on TAdvPageControl to emulate Delphi 2005 tab look- New: drag & drop code sample for TAdvPageControl addedUpdate : TAdvOutlookList ...
特征应用程序可以在本地http服务器上运行,也可以模拟/发布到Android / iOS 很多有用的gulp任务,例如: gulp监视更改+浏览器同步(带有livereload的http服务器)+ weinre调试器gulp cordova:emulate:ios在iOS模拟器...
**CGI::PSGI与p5-cgi-emulate-psgi** 在Web开发领域,CGI(Common Gateway Interface)和PSGI(Perl Scalable Gateway Interface)是两种不同的接口标准,用于连接Perl脚本与Web服务器。理解它们的概念以及它们...
EMULATE DONGLE Mkbus driver
THIS PROGRAM FOR EMULATE DONGLE
这个"swp_emulate.rar_swp"文件集似乎关注的是通过ldrex/strex指令序列来模拟SWP指令的功能。这里我们将深入探讨SWP指令、ldrex/strex指令以及它们在错误检查中的应用。 首先,SWP(Swap)指令是早期ARM处理器中的...
Emulate仿真模拟教材PPT课件.pptx
You learn to simulate networks with NS-3, and emulate networking systems with Mininet and its extensions. Chapter 10, Getting Started with SDN, discusses the enterprise SDN controllers, configuring ...