java 如何在枚举中调用附加方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5959965/
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
How to call additional method in enums?
提问by ponds
enum Enum1
{
BIG(8), HUGE(10)
{
public String getName()
{
return "Huge";
}
public String getContry()
{
return "India";
}//additional Method
},
OVERWHELMING(16)
{
public String getName()
{
return "OVERWHELMING";
}
};
private int ounces;
public int getOunes()
{
return ounces;
}
public String getName()
{
return "Ponds";
}
Enum1(int ounces1)
{
ounces = ounces1;
}
}
class EnumAsInnerClass
{
Enum1 enumInnerClass;
public static void main(String[] args)
{
EnumAsInnerClass big = new EnumAsInnerClass();
big.enumInnerClass = Enum1.BIG;
EnumAsInnerClass over = new EnumAsInnerClass();
over.enumInnerClass = Enum1.OVERWHELMING;
EnumAsInnerClass huge = new EnumAsInnerClass();
huge.enumInnerClass = Enum1.HUGE;
System.out.println(big.enumInnerClass.getName());//Ponds
System.out.println(over.enumInnerClass.getName());//OVERWHELMING
System.out.println(huge.enumInnerClass.getName());//Huge
}
}
Consider the above example. How can I call the method getCountry for HUGE?
考虑上面的例子。如何为 HUGE 调用 getCountry 方法?
If there is no way to call this method, why does Java treat it as legal?
如果没有办法调用这个方法,那Java为什么把它当作合法的呢?
回答by Jon Skeet
(As noted nearly 4 years later, my original answer was incorrect.)
(正如近 4 年后指出的,我最初的答案是不正确的。)
You can't even call Enum1.HUGE.getCountry()
with the specific enum, which is very slightly surprising... but even if you could, you couldn't do so with an arbitrary Enum1
value, which would be more generally useful.
您甚至不能Enum1.HUGE.getCountry()
使用特定的枚举进行调用,这有点令人惊讶……但即使可以,也不能使用任意Enum1
值来调用,这会更普遍有用。
The solution is to stop it from being an additional method - add a getCountry()
method into Enum1
, either returning a default value or maybe throwing an exception. HUGE
can then override the method.
解决方案是阻止它成为一个额外的方法——在 中添加一个getCountry()
方法Enum1
,要么返回一个默认值,要么抛出一个异常。HUGE
然后可以覆盖该方法。
It would be nice if you could declare that a particular enum value implemented an interface, but it seems there's no syntax for that.
如果你能声明一个特定的枚举值实现了一个接口,那就太好了,但似乎没有语法。
回答by Alan Escreet
The enum
is a type definition, similar to a class, so Enum1
doesn't have the method getCountry()
available in its interface.
这enum
是一个类型定义,类似于类,因此在其接口Enum1
中没有getCountry()
可用的方法。
edit:Alternative solution
编辑:替代解决方案
What you cando is define the required methods in Enum1
and override them in the individual enum values.
您可以做的是定义所需的方法Enum1
并在各个枚举值中覆盖它们。
回答by uvsmtid
If there is no way to call this method then why do java treat as legal?
如果没有办法调用这个方法,那么为什么java将其视为合法的?
You can still call it within enum value definition. For example, change getName()
function:
您仍然可以在枚举值定义中调用它。例如更改getName()
函数:
// ...
HUGE(10)
{
public String getName()
{
// Call additional Method
return "Huge" + getCountry();
}
public String getContry()
{
return "India";
} // additional Method
},
// ...
Otherwise, you need to override by it to be able to call it.
否则,您需要覆盖它才能调用它。
回答by MC Emperor
This is not suprising at all.
这一点也不令人惊讶。
When you define an enum
type (in your case Enum1
) and declare values for it, their reference type will be of the defined type (Enum1
). When you define methods within your enum
declarations, you have just created anonymous subclasses.
当您定义一个enum
类型(在您的情况下Enum1
)并为其声明值时,它们的引用类型将是定义的类型 ( Enum1
)。当您在enum
声明中定义方法时,您刚刚创建了匿名子类。
public enum Enum1 {
OVERWHELMING(16) {
public String getName() {
return "OVERWHELMING";
}
};
// And the remaining constructors, fields and methods
}
And you can't just normallycall methods of an anonymous class from outside the definition itself. Likewise, someOtherMethod()
will not be accessible from the outside either:
而且您不能只是从定义本身之外正常调用匿名类的方法。同样,someOtherMethod()
也无法从外部访问:
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
someOtherMethod();
}
public void someOtherMethod() {
System.out.println("Some other method!");
}
};
Such methods are, however, accessible from the declaration itself, like stated in this answer.
但是,可以从声明本身访问此类方法,如本答案中所述。
Note: Like Joachim Sauersaid in his comment, this might not be the best design. You should, for example, move your methods to the Enum1
type.
注意:就像Joachim Sauer在他的评论中所说的那样,这可能不是最好的设计。例如,您应该将您的方法移动到Enum1
类型。
There is stilla way. It is possible to access the method through reflection:
目前仍有一种方式。可以通过反射访问该方法:
Class<?> clazz = Enum1.HUGE.getClass();
Method method = clazz.getMethod("getCountry");
System.out.println(method.invoke(Enum1.HUGE)); // Prints "India"