參考資料
http://en.wikipedia.org/wiki/Java_annotation
http://docs.oracle.com/javase/tutorial/java/annotations/
Annotation 中文稱為註解
在Java language中 Annotation有以下幾種
Annotations applied to Java code:
@Override- Checks that the method is an override. Causes a compile error if the method is not found in one of the parent classes or implemented interfaces.
@Deprecated - Marks the method as obsolete. Causes a compile warning if the method is used.
@SuppressWarnings - Instructs the compiler to suppress the compile time warnings specified in the annotation parameters.
@SafeVarargs - Suppress warnings for all callers of a method or constructor with a generics varargs parameter, since Java 7.
@FunctionalInterface - Specifies that the type declaration is intended to be a functional interface, since Java 8.
Annotations applied to other annotations:
@Retention - Specifies how the marked annotation is stored—Whether in code only, compiled into the class, or available at runtime through reflection.
@Documented - Marks another annotation for inclusion in the documentation.
@Target - Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
@Inherited - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited to subclasses).
@Repeatable - Specifies that the annotation can be applied more than once to the same declaration.
其中在開發Android APP最常見的莫過於@Override。我們需要覆寫某個父類別的方法時,可以在Method前加上@Override,如此編譯器在編譯時會確認覆寫方法的命名,是否與父類別一致,如果不一致便會產生Error提醒我們,因為名稱不一致就沒有達到覆寫的目的。
沒有留言:
張貼留言