2010-04-01 51 views
0

這段代碼中這些java.lang.annotation導入的目的是什麼?爲什麼他們需要定義MyAnnotation?爲什麼MyAnnotation的定義需要記錄,繼承,保留和保留策略?

import java.lang.annotation.Documented; 
import java.lang.annotation.Inherited; 
import java.lang.annotation.Retention; 
import java.lang.annotation.RetentionPolicy; 

@Documented 
@Retention(RetentionPolicy.RUNTIME) 
@Inherited 
public @interface MyAnnotation { 
    String value() default ""; 
} 
+0

請參閱http://stackoverflow.com/a/20432882/715269瞭解Documented and Inherited的說明。 – Gangnus 2013-12-06 20:26:40

回答

1

@Documented和@Inherited不是必需的。 @Retention(RetentionPolicy.RUNTIME)僅在您想要在運行時處理批註時才需要。