第17章 Annotation Annotation meta-annotation
第17章 • Annotation – Annotation – meta-annotation
限定 Override父类方法@ Override 对编译程序说明某个方法必须是重新定义 父类别中的方法 public class CustomClass i @ Override public stringtostring ()t return cus tomobjecti CustomClass. java: 4: method does not override a method from its superclass @override M1 error
限定Override父类方法@Override • 对编译程序说明某个方法必须是重新定义 父类别中的方法 public class CustomClass { @Override public StringToString() { return "customObject"; } } CustomClass.java:4: method does not override a method from its superclass @Override ^1 error
限定 Override父类方法@ Override java. lang Override是个 Marker annotation 用于标示的 Annotation, Annotation名称本 身即表示了要给工具程序的信息
限定Override父类方法@Override • java.lang.Override是个Marker annotation • 用于标示的Annotation,Annotation名称本 身即表示了要给工具程序的信息
标示方法為 Deprecated@ Deprecated 对编译程序说明某个方法已经不建议使用 public class Something t @Deprecated public Something getsomething()i return new Something() Something some new Something ()i //呼叫被 @Deprecated标示的方法 some get something()i javac -Xlint: deprecation -d. SomethingDemo java Something Demo. java: 6: warning: [deprecation] getsomething() in onlyfun caterpillar Something has been deprecated some get something ()i A1 warning
标示方法為Deprecated @Deprectated • 对编译程序说明某个方法已经不建议使用 public class Something { @Deprecated public Something getSomething() { return new Something(); } } Something some = new Something(); //呼叫被@Deprecated标示的方法 some.getSomething(); javac -Xlint:deprecation -d . SomethingDemo.java SomethingDemo.java:6: warning: [deprecation] getSomething() in onlyfun.caterpillar.Something has been deprecated some.getSomething(); ^1 warning
标示方法为 Deprecated@ Deprecated java. lang. Deprecated也是個 Marker annotation Deprecated这个名称在告知编译程序,被 @ Deprecated标示的方法是一个不建议被 使用的方法
标示方法为Deprecated @Deprectated • java.lang.Deprecated也是個Marker annotation • Deprecated这个名称在告知编译程序,被 @Deprecated标示的方法是一个不建议被 使用的方法
扣制编译程序警讯@ SuppressWarnings 对编译程序说明某个方法中若有警示讯息 则加以抑制 import java.util.* public class SomeClass public void do Something () Map map new HashMap()i map. put( some",thing")i Note: Some Class. java uses unchecked or unsafe operations Note: Recompile with -Xlint: unchecked for details Gavac-xlint: unchecked -d. Some Class. java Some Class. java: 8: warning: [unchecked] unchecked call to put(K,v) as a member of the raw type java util. Map map. put("some",thing")i 1 warning
抑制编译程序警讯@SuppressWarnings • 对编译程序说明某个方法中若有警示讯息, 则加以抑制 import java.util.*; public class SomeClass { public void doSomething() { Map map = new HashMap(); map.put("some", "thing"); } } Note: SomeClass.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. javac-Xlint:unchecked -d . SomeClass.java SomeClass.java:8: warning: [unchecked] unchecked call to put(K,V) as a member of the raw type java.util.Map map.put("some", "thing"); ^1 warning
抑制编译程序警讯@ SuppressWarnings import java.util. x public class someclass 2 G Suppresswarnings(value=("unchecked"F) public void do Something ( Map map new HashMap ()i map. put("some",thing") @SuppressWarnings(value=["unchecked","deprecation")
抑制编译程序警讯@SuppressWarnings import java.util.*; public class SomeClass2 { @SuppressWarnings(value={"unchecked"}) public void doSomething() { Map map = new HashMap(); map.put("some", "thing"); } } @SuppressWarnings(value={"unchecked", "deprecation"})
自定义 Annotation型态 定乂 Marker annotation,也就是 Annotation 名称本身即提供信息 对于程序分析工具来说,主要是检查是否 有 MarkerAnnotation的出现,并作出对应的 动作 publiceinterface Debug (] public class SomeObject @Debug public void do Something ( //
自定义Annotation型态 • 定义Marker Annotation,也就是Annotation 名称本身即提供信息 • 对于程序分析工具来说,主要是检查是否 有MarkerAnnotation的出现,并作出对应的 动作 public@interface Debug {} public class SomeObject { @Debug public void doSomething() { // .... } }
自定义 Annotation型态 Single-value annotation public@interface UnitTest i String value ()i public class MathTool i @Unitrest(GCD") public static int gcdof (int numl, int num2
自定义Annotation型态 • Single-value annotation public@interface UnitTest { String value(); } public class MathTool { @UnitTest("GCD") public static int gcdOf(int num1, int num2) { // .... } }
自定义 Annotation型态 public@interface FunctionTest String[ value ()i 简便形式 @FunctionTest(("methodl","method2") 详细形式 @FunctionTest(value=(methodl"method2") vaue成员设定默认值,用" default"尖键词 public @interface UnitTest2 i String value() default noMethod"i
自定义Annotation型态 public@interface FunctionTest { String[] value(); } @FunctionTest({"method1", "method2"}) • 简便形式 • 详细形式 • value成员设定默认值,用"default"关键词 @FunctionTest(value={"method1", "method2"}) public @interface UnitTest2 { String value() default "noMethod"; }