我可以在 Java 中向枚举添加函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2457076/
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
Can I add a function to enums in Java?
提问by Samuel Carrijo
I have an enum, which looks like
我有一个枚举,看起来像
public enum Animal {
ELEPHANT,
GIRAFFE,
TURTLE,
SNAKE,
FROG
}
and I want to do something like
我想做类似的事情
Animal frog = Animal.FROG;
Animal snake = Animal.SNAKE;
boolean isFrogAmphibian = frog.isAmphibian(); //true
boolean isSnakeAmphibian = snake.isAmphibian(); //false
boolean isFrogReptile = frog.isReptile(); //false
boolean isSnakeReptile = snake.isReptile(); //true
boolean isFrogMammal = frog.isMammal(); //false
boolean isSnakeMammal = snake.isMammal(); //false
I simplified the example for didactic purposes, but this would be really useful to me for my real life example. Can I do it in Java?
我出于教学目的简化了示例,但这对我的现实生活示例非常有用。我可以用Java做吗?
采纳答案by danben
Yes Enum is a class in Java:
是的 Enum 是 Java 中的一个类:
public enum Animal
{
ELEPHANT(true),
GIRAFFE(true),
TURTLE(false),
SNAKE(false),
FROG(false);
private final boolean mammal;
private Animal(final boolean mammal) { this.mammal = mammal; }
public boolean isMammal() { return this.mammal; }
}
but in your case for a real system I would make that an Enum as well since there is a fixed set of types of animals.
但是在您的真实系统中,我也会将其设为 Enum,因为有一组固定的动物类型。
public enum Type
{
AMPHIBIAN,
MAMMAL,
REPTILE,
BIRD
}
public enum Animal
{
ELEPHANT(Type.MAMMAL),
GIRAFFE(Type.MAMMAL),
TURTLE(Type.REPTILE),
SNAKE(Type.REPTILE),
FROG(Type.AMPHIBIAN);
private final Type type;
private Animal(final Type type) { this.type = type; }
public boolean isMammal() { return this.type == Type.MAMMAL; }
public boolean isAmphibian() { return this.type == Type.AMPHIBIAN; }
public boolean isReptile() { return this.type == Type.REPTILE; }
// etc...
}
Also note that it is important to make any instance variable final
as well.
另请注意,使任何实例变量也很重要final
。
You can find more details about it in the Java Language Specification.
您可以在Java 语言规范 中找到有关它的更多详细信息。
回答by danben
Yes, you can. It would look like this:
是的你可以。它看起来像这样:
public enum Animal {
ELEPHANT(false),
GIRAFFE(false),
TURTLE(false),
SNAKE(false),
FROG(true);
private final boolean isAmphibian;
Animal(boolean isAmphibian) {
this.isAmphibian = isAmphibian;
}
public boolean isAmphibian() {
return this.isAmphibian;
}
}
Then you would call it like:
然后你会这样称呼它:
Animal.ELEPHANT.isAmphibian()
Animal.ELEPHANT.isAmphibian()
回答by rghome
As well as using the techniques above which add a field to the enumerated type you can also use a pure method based approach and polymorphism. This is more "OOP style" but I would not say it is necessarily better.
除了使用上述向枚举类型添加字段的技术外,您还可以使用基于纯方法的方法和多态性。这更像是“OOP 风格”,但我不会说它一定更好。
Unfortunately, you may (see the comment below) need to define an interface:
不幸的是,您可能(请参阅下面的评论)需要定义一个接口:
public interface AnimalTraits {
default boolean isAmphibian() { return false; };
default boolean isReptile() { return false; };
default boolean isMammal() { return false; };
}
But then you can them implement the interface in each of your enumeration elements:
但是你可以在你的每个枚举元素中实现接口:
public enum Animal implements AnimalTraits {
ELEPHANT { @Override public boolean isMammal() { return true; } },
GIRAFFE { @Override public boolean isMammal() { return true; } },
TURTLE { @Override public boolean isReptile() { return true; } },
SNAKE { @Override public boolean isReptile() { return true; } },
FROG { @Override public boolean isAmphibian() { return true; } }
}
Note that I use default implementations in the interface to cut down on the amount of typing you need in the enum.
请注意,我在界面中使用默认实现来减少您在枚举中需要的输入量。
Regarding the necessity of the interface: I tried adding the methods in the interface as abstract methods at the top of the enum and Eclipse seemed to allow it and insisted on implementations in the enum elements, but then failed to compile those properly. So it looks like it ought to be possible without an interface, but perhaps it is not yet implemented in the compiler.
关于接口的必要性:我尝试将接口中的方法添加为枚举顶部的抽象方法,Eclipse 似乎允许它并坚持在枚举元素中实现,但随后未能正确编译这些方法。所以看起来没有接口应该是可能的,但也许它尚未在编译器中实现。
回答by Emaborsa
I have an other option:
我还有一个选择:
public enum Animal {
ELEPHANT {
@Override
boolean isMammal() {
return true;
};
@Override
boolean isReptile() {
return false;
}
},
SNAKE {
@Override
boolean isMammal() {
return false;
};
@Override
boolean isReptile() {
return true;
}
};
abstract boolean isMammal();
abstract boolean isReptile();
}
No need of external Interface and I am quite sure (did not test) it works also on Java7.
不需要外部接口,我很确定(没有测试)它也适用于 Java7。