简体   繁体   English

我可以将方法参数限制为仅某些Enum成员吗?

[英]Can I restrict method parameters to only certain Enum members?

Is there a way to restrict a method to only take certain members of an enumeration. 有没有一种方法可以限制只接受枚举的某些成员的方法。 Let's say you had an enumeration of military and enlisted ranks. 假设您列举了一些军事人员和应征人员。 If I wanted a function that only could take officer ranks and another that could only take enlisted ranks, I don't think there's a way to do that in Java, is there? 如果我想要一个仅能获得军官级别的功能而另一个仅能获得入伍军官级别的功能,我认为Java中没有办法做到这一点,是吗?

Your only option is to use distinct enums, as a method taking an enum as a parameter will take any of its values at compile time. 唯一的选择是使用不同的枚举,因为将枚举作为参数的方法将在编译时使用其任何值。

IDEs help with the typing. IDE帮助您进行键入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM