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程序。这款仿真软件...
《Emulate5000仿真模拟教材》是针对SLC5000系列控制器的一份详细教程,旨在帮助用户在不实际操作硬件的情况下,通过软件模拟进行程序调试和系统测试。SLC5000是Allen Bradley(罗克韦尔自动化)推出的一种高级微处理...
Emulate5000应用之二(logix5000仿真软件) Emulate5000应用之二(logix5000仿真软件)
资源分类:Python库 所属语言:Python 资源全名:iotile_emulate-0.5.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Emulate仿真模拟教材PPT学习教案 Emulate仿真模拟教材PPT学习教案是关于RSLogix Emulate 5000 Logix5000处理器模拟软件的学习教案。该教案主要介绍了Emulate 5000的基础知识和应用场景,并提供了详细的操作步骤和...
emulate5000使用录像,资源共享,共同学习进步!
【标题】"Emulate5000仿真模拟教材_ab_" 提供的是关于使用Emulate5000软件进行AB PLC(Allen Bradley Programmable Logic Controller)仿真的教学资源。这个仿真模拟教程是专为那些想在没有实际设备的情况下学习和...
RSLogix_Emulate500使用教程
【标题】:“emulate.c:具有几个基本说明的简单虚拟机” 这个标题指的是一个名为“emulate.c”的源代码文件,它实现了一个简单的虚拟机。虚拟机是一种软件模拟,能够执行特定指令集的程序,而这些指令集并不直接...
Emulate5000仿真模拟教材ppt,Emulate5000仿真模拟教材
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服务器。理解它们的概念以及它们...