枚举在类范围之外声明

发布于 2024-08-07 05:42:42 字数 441 浏览 3 评论 0原文

我参加了这次软件开发人员职位的面试,他们给了我一些极端情况代码情况的测试,通常有 4 个选项可供选择。
其中一个问题在类范围之外声明了一个枚举,我立即检查了“不编译”答案并继续解决其他问题。 它是这样的:

enum Colors {BLUE,RED,GREEN}

class Test {
    //other code, not really important with my question
}

这段代码实际上可以编译。
除了这样的面试(可能或)可能对确定一个人是否是一名优秀的开发人员没有用这一事实之外,让我担心的是:为什么我要声明这样的枚举?为什么我只能用枚举来做到这一点? 我做了一些测试,发现它在班级内部可见,但对其他班级不可见。

旁注:我的成绩很差:P。我得到了理论上的最大值,但在极端情况代码情况下接近最低的可能性。我认为我不会得到这份工作。

I went to this interview for a software developer position and they gave me a test with some corner-case-code situations, with usually 4 options to choose.
One of the questions had an enum declared outside the class scope, I promptly checked the "does not compile" answer and went ahead with the other questions.
It was something like:

enum Colors {BLUE,RED,GREEN}

class Test {
    //other code, not really important with my question
}

This code actually compiles.
Besides the fact that an interview like this (might or) might not be useful to find out if one is a good developer, what worries me is: why would I declare an enum like this? Why I can only do this with enum?
I did some testing and found out that it is visible inside the class, but not to other classes.

Sidenote: I scored really poor :P. I got the max on the theory but near the lowest possibile on the corner-case-code situations. I don't think I'll get the job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

三人与歌 2024-08-14 05:42:42

这不仅仅是枚举。枚举只是特殊类型的类。一般来说,您可以在一个文件中声明多个类(只要其中没有两个类是公共的)。

It's not just enums. Enums are just special kinds of classes. In general you can have multiple classes declared in one file (as long as no two of them are public).

╰沐子 2024-08-14 05:42:42

不,如果没有访问修饰符,枚举是包私有的。这意味着它只能由同一包中的类使用。您不仅可以使用枚举来做到这一点,还可以将类设为包私有。

更多信息: http://java.sun.com/docs/ books/tutorial/java/javaOO/accesscontrol.html

No, without an access modifier, the enum is package-private. This means it can only be used by classes in the same package. And you can't only do this with an enum, classes can also be made package-private.

More info: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

梦里兽 2024-08-14 05:42:42

有时这种习惯用法可能是明智的 - 例如,假设您有一个 UploadHandler 类(或类似的东西),它可以从上传返回状态。对我来说,将此状态实现为枚举是相当可行的 - 并且由于枚举(例如 UploadStatus)显然“属于”UploadHandler 类,因此在同一个源文件中声明它似乎很好。 (当然,这确实假设它只需要是包私有的 - 如果它真正是公共的,则需要在自己的文件中声明,如果它不再是内部的东西,这可能是有意义的)。

碰巧,在这种情况下,我可能会将其设为静态内部类,以使关系更加明确。但是在同一个源文件中声明多个类并不总是坏事,有时可以通过设置这是一个边缘琐碎的附属类的期望来帮助提高可读性。 (出于同样的原因,我认为这样的类不应该做任何特别复杂或意外的事情。)

Sometimes this idiom can be sensible - for example, imagine you have an UploadHandler class (or something like that) which can return a status from an upload. It seems quite feasible to me to implement this status as an enum - and since the enum (e.g. UploadStatus) clearly "belongs" to the UploadHandler class, it seems fine to declare it in the same source file. (This does assume of course that it only needs to be package-private - if it's truly public it would need to be declared in its own file, which would probably make sense if it's not an internal thing any more).

As it happens, in this case I would probably make it a static inner class to make the relationship more explicit. But declaring multiple classes in the same source file isn't always bad and can sometimes help readability by setting the expectation that this is a borderline-trivial, subsidiary class. (By the same token, I don't think classes like this should do anything particularly complex or unexpected.)

卷耳 2024-08-14 05:42:42

它实际上在我的 Eclipse 上编译! ;-)

多个类可以位于同一个文件中。限制是公共类必须在同名的文件中定义。

它的可见性是“包”,因此它在同一包中的其他类中也应该是可见的。

我可以用该枚举做什么?

您可以在上述限制下做任何您想做的事情......

注意:虽然你错了,但你不应该感到太难过,因为这也不是一个好的做法。在我们的 CheckStyle 配置中,像这样的同一文件中的外部类将被视为错误!

It compiles actually, on my Eclipse ! ;-)

Several classes are allowed to be in the same file. The limitation is that a public class has to be defined in a file that has the same name.

It's visibility is 'package', so it should be visible in other classes in the same package too.

What can I do with that enum?

You can do anything you want with the above limitations...

Note : although you had it wrong, you shouldn't feel too bad, because it's not really a good practice either. In our CheckStyle configuration, outer classes in the same file like this are treated as errors !!

迷迭香的记忆 2024-08-14 05:42:42

枚举指定可以分配给特定类型的常量值列表。
它可以在班级内部或外部。

An enum specifies a list of constant values that can be assigned to a particular type.
It can be either inside or outside of the class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文