java 枚举在类范围之外声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1503040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:48:12  来源:igfitidea点击:

Enum declared outside class scope

javaenums

提问by Alberto Zaccagni

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:

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

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.

旁注:我的得分真的很差:P。我得到了理论的最大值,但在角落案例代码情况下接近最低的可能性。我不认为我会得到这份工作。

采纳答案by newacct

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).

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

回答by Bart Kiers

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

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

回答by Andrzej Doyle

Sometimes this idiom can be sensible - for example, imagine you have an UploadHandlerclass (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).

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

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.)

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

回答by Madhu

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.

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

回答by KLE

It compiles actually, on my Eclipse ! ;-)

它实际上在我的 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 practiceeither. In our CheckStyle configuration, outer classes in the same file like this are treated as errors !!

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