Java 枚举定义 [英] Java Enum definition

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

问题描述

我以为我很了解 Java 泛型,但后来我在 java.lang.Enum 中发现了以下内容:

I thought I understood Java generics pretty well, but then I came across the following in java.lang.Enum:

class Enum<E extends Enum<E>>

有人能解释一下如何解释这个类型参数吗?提供可以使用类似类型参数的其他示例的奖励积分.

Could someone explain how to interpret this type parameter? Bonus points for providing other examples of where a similar type parameter could be used.

推荐答案

这意味着 enum 的类型参数必须从本身具有相同类型参数的 enum 派生.这怎么会发生?通过使类型参数成为新类型本身.因此,如果我有一个名为 StatusCode 的枚举,则相当于:

It means that the type argument for enum has to derive from an enum which itself has the same type argument. How can this happen? By making the type argument the new type itself. So if I've got an enum called StatusCode, it would be equivalent to:

public class StatusCode extends Enum<StatusCode>

现在,如果您检查约束,我们会得到 Enum - 所以 E=StatusCode.让我们检查一下:E 是否扩展了 Enum?是的!我们没事.

Now if you check the constraints, we've got Enum<StatusCode> - so E=StatusCode. Let's check: does E extend Enum<StatusCode>? Yes! We're okay.

您可能会问自己这是什么意思:) 嗯,这意味着 Enum 的 API 可以引用自身 - 例如,能够说 Enum实现 Comparable.基类能够进行比较(在枚举的情况下),但它可以确保它只比较正确类型的枚举.(嗯,几乎 - 请参阅底部的编辑.)

You may well be asking yourself what the point of this is :) Well, it means that the API for Enum can refer to itself - for instance, being able to say that Enum<E> implements Comparable<E>. The base class is able to do the comparisons (in the case of enums) but it can make sure that it only compares the right kind of enums with each other. ( Well, nearly - see the edit at the bottom.)

我在 ProtocolBuffers 的 C# 端口中使用了类似的东西.有消息"(不可变)和构建器"(可变,用于构建消息)——它们以类型对的形式出现.涉及的接口有:

I've used something similar in my C# port of ProtocolBuffers. There are "messages" (immutable) and "builders" (mutable, used to build a message) - and they come as pairs of types. The interfaces involved are:

public interface IBuilder<TMessage, TBuilder>
  where TMessage : IMessage<TMessage, TBuilder> 
  where TBuilder : IBuilder<TMessage, TBuilder>

public interface IMessage<TMessage, TBuilder>
  where TMessage : IMessage<TMessage, TBuilder> 
  where TBuilder : IBuilder<TMessage, TBuilder>

这意味着您可以从消息中获得适当的构建器(例如,获取消息的副本并更改某些位),而在构建完成后,您可以从构建器中获得适当的消息.不过,API 的用户实际上并不需要关心这一点,这是一件好事 - 它非常复杂,并且需要多次迭代才能达到它的位置.

This means that from a message you can get an appropriate builder (e.g. to take a copy of a message and change some bits) and from a builder you can get an appropriate message when you've finished building it. It's a good job users of the API don't need to actually care about this though - it's horrendously complicated, and took several iterations to get to where it is.

请注意,这不会阻止您创建使用类型参数的奇数类型,该类型参数本身是可以的,但类型不同.目的是在正确情况下提供好处,而不是保护您免受错误情况的影响.

Note that this doesn't stop you from creating odd types which use a type argument which itself is okay, but which isn't the same type. The purpose is to give benefits in the right case rather than protect you from the wrong case.

因此,如果 Enum 无论如何都没有在 Java 中专门"处理,您可以(如注释中所述)创建以下类型:

So if Enum weren't handled "specially" in Java anyway, you could (as noted in comments) create the following types:

public class First extends Enum<First> {}
public class Second extends Enum<First> {}

Second 将实现 Comparable 而不是 Comparable... 但是 First 本身会好的.

Second would implement Comparable<First> rather than Comparable<Second>... but First itself would be fine.

这篇关于Java 枚举定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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