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

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

问题描述

我刚刚发现Java允许枚举实现一个接口。这将是一个很好的用例。

解决方案

枚举不仅仅是表示被动集(如颜色)。它们可以用功能代表更复杂的对象,因此您可能希望为其添加更多功能。您可能有接口,例如可打印可报告等和支持这些的组件。


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

解决方案

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天全站免登陆