为什么枚举会实现接口? [英] Why would an Enum implement an Interface?

查看:21
本文介绍了为什么枚举会实现接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现 Java 允许枚举实现接口.什么是好的用例?

I just found out that Java allows enums to implement an interface. What would be a good use case for that?

推荐答案

枚举不仅仅代表被动集合(例如颜色).它们可以表示具有功能的更复杂的对象,因此您可能希望为这些对象添加更多功能 - 例如您可能有诸如 PrintableReportable 等接口和支持这些的组件.

Enums don't just have to represent passive sets (e.g. colours). They can represent more complex objects with functionality, and so you're then likely to want to add further functionality to these - e.g. you may have interfaces such as Printable, Reportable etc. and components that support these.

这篇关于为什么枚举会实现接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆