Enum 상수는 아래의 예제 하나면 끝.
public enum Level { HIGH (3), //calls constructor with value 3 MEDIUM(2), //calls constructor with value 2 LOW (1) //calls constructor with value 1 ; // semicolon needed when fields / methods follow private final int levelCode; Level(int levelCode) { this.levelCode = levelCode; } public int getLevelCode() { return this.levelCode; } }
자바의 Annotation 은 크게 3가지 방식으로 사용이 가능한데, Compile, Build, Runtime 시점에 활용할 수가 있다. Runtime 시는 아래와 같다.
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { public String name(); public String value(); }
사용 시에는 반드시 아래와 같이 명시적인 Casting 이 필요하다. Reflection !!!
Class aClass = TheClass.class;
Annotation[] annotations = aClass.getAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("name: " + myAnnotation.name());
System.out.println("value: " + myAnnotation.value());
}
}
'프로그래밍 > java' 카테고리의 다른 글
Jenkov.com - Java Lambda Expressions (0) | 2015.04.27 |
---|---|
Jenkov.com - Java Interfaces (0) | 2015.04.21 |
Jenkov.com - Java Abstract Class (0) | 2015.04.21 |