| java.lang.Deprectated是J2SE
5.0中標準的Annotation型態之一,它對編譯器說明某個方法已經不建議使用,如果有人試圖使用或重新定義該方法,必須提出警示訊息。 舉個例子來說,您可能定義一個CustomObject類別,並在當中定義有getSomething()方法,而在一段時間之後,您不建議使用這個方法 了,並要將這個方法標示為deprectated,您可以這麼作 :
public class CustomObject {
如果有人試圖在繼承這個類別後重新定義getSomething(),或是在程式中呼叫使用getSomething()方法,則進行編譯時,就會出現這 個警訊: Note: SubCustomObject.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details. 想要知道詳細的警訊內容的話,可以在編譯時加上-Xline:deprecation引數,例如: >javac -Xlint:deprecation SubCustomObject.java
SubCustomObject.java:5: warning: [deprecation] getSomething() in CustomObject ha s been deprecated object.getSomething(); ^ 1 warning java.lang.Deprecated是個Marker annotation,簡單的說就是用於標示,annotation名稱本身即包括了要給工具程式的資訊。 |