Java의 열거 형에 함수를 추가 할 수 있습니까?
다음과 같은 열거 형이 있습니다.
public enum Animal {
ELEPHANT,
GIRAFFE,
TURTLE,
SNAKE,
FROG
}
그리고 나는 다음과 같은 것을하고 싶다.
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
교훈적인 목적으로 예제를 단순화했지만 실제 예제에서는 정말 유용 할 것입니다. 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; }
}
그러나 실제 시스템의 경우 고정 된 유형의 동물이 있기 때문에 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...
}
또한 인스턴스 변수를 만드는 것도 중요합니다 final
.
그래 넌 할수있어. 다음과 같이 표시됩니다.
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;
}
}
그런 다음 다음과 같이 호출합니다.
Animal.ELEPHANT.isAmphibian()
열거 형에 필드를 추가하는 위의 기술을 사용할뿐만 아니라 순수한 방법 기반 접근 방식과 다형성을 사용할 수도 있습니다. 이것은 더 "OOP 스타일"이지만 반드시 더 낫다고 말할 수는 없습니다.
불행히도 인터페이스를 정의해야합니다.
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.
Note: requires Java 8 or above.
참고URL : https://stackoverflow.com/questions/2457076/can-i-add-a-function-to-enums-in-java
'IT TIP' 카테고리의 다른 글
좋아하는 SQLPLUS 팁과 요령 (0) | 2020.12.04 |
---|---|
C ++에서 클래스를 다른 클래스에서 상속하는 것으로 앞으로 선언 할 수 있습니까? (0) | 2020.12.04 |
상속을 사용하는 이유는 무엇입니까? (0) | 2020.12.04 |
nginx 502 잘못된 게이트웨이 (0) | 2020.12.04 |
함수를 호출 한 요소의 ID 가져 오기 (0) | 2020.12.04 |