摘要:知識點(diǎn)總結(jié)注解內(nèi)置注解知識點(diǎn)總結(jié)注解定義在中,此注釋只適用于修飾方法,表示一個方法聲明打算重寫父類的另一個方法聲明。此注釋可用于修飾方法屬性類,表示不鼓勵程序員使用這樣的元素,通常是因為它很危險或存在更好的選擇。
Java知識點(diǎn)總結(jié)(注解-內(nèi)置注解)
@(Java知識點(diǎn)總結(jié))[Java, 注解]
@Override定義在java.lang.Override 中,此注釋只適用于修飾方法,表示一個方法聲明打算重寫父類的另一個方法聲明。
public class Demo01 { @Override public String toString() { return ""; } }
源碼
import java.lang.annotation.*; /** * Indicates that a method declaration is intended to override a * method declaration in a supertype. If a method is annotated with * this annotation type compilers are required to generate an error * message unless at least one of the following conditions hold: * *
定義在java.lang.Deprecated中,遺棄、廢棄,不建議使用。此注釋可用于修飾方法、屬性、類,表示不鼓勵程序員使用這樣的元素,通常是因為它很危險或存在更好的選擇。
public class Demo01 { @Deprecated public static void test1(){ } public static void main(String[] args) { test1(); } }
源碼
import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; /** * A program element annotated @Deprecated is one that programmers * are discouraged from using, typically because it is dangerous, * or because a better alternative exists. Compilers warn when a * deprecated program element is used or overridden in non-deprecated code. * * @author Neal Gafter * @since 1.5 * @jls 9.6.3.6 @Deprecated */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @interface Deprecated { }@SuppressWarings
定義在java.lang.SuppressWarnings中,用來抑制編譯時的警告信息。
與前兩個注釋有所不同, 你需要添加一個參數(shù)才能正確使用 ,這些參數(shù)值是已經(jīng)定義好了的,我們選擇性的使用就好了,參數(shù)如下:
參數(shù) | 說明 |
---|---|
deprecation | 使用了過時的類或方法的警告 |
unchecked | 執(zhí)行了未檢查的轉(zhuǎn)換時的警告,如使用集合時未指定泛型 |
fallthrough | 當(dāng)在switch語句使用時發(fā)生case穿透 |
path | 在類路徑、源文件路徑等中有不存在路徑的警告 |
serial | 當(dāng)在可序列化的類上缺少serialVersionUID定義時的警告 |
finally | 任何finally子句不能完成時的警告 |
all | 關(guān)于以上所有情況的警告 |
@SuppressWarnings("unchecked") @SuppressWarnings(value={"unchecked","deprecation"})
import java.util.ArrayList; import java.util.List; public class Demo01 { @SuppressWarnings("all") public static void test2(){ List list = new ArrayList(); }
源碼
package java.lang; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; /** * Indicates that the named compiler warnings should be suppressed in the * annotated element (and in all program elements contained in the annotated * element). Note that the set of warnings suppressed in a given element is * a superset of the warnings suppressed in all containing elements. For * example, if you annotate a class to suppress one warning and annotate a * method to suppress another, both warnings will be suppressed in the method. * *As a matter of style, programmers should always use this annotation * on the most deeply nested element where it is effective. If you want to * suppress a warning in a particular method, you should annotate that * method rather than its class. * * @author Josh Bloch * @since 1.5 * @jls 4.8 Raw Types * @jls 4.12.2 Variables of Reference Type * @jls 5.1.9 Unchecked Conversion * @jls 5.5.2 Checked Casts and Unchecked Casts * @jls 9.6.3.5 @SuppressWarnings */ @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) @Retention(RetentionPolicy.SOURCE) public @interface SuppressWarnings { /** * The set of warnings that are to be suppressed by the compiler in the * annotated element. Duplicate names are permitted. The second and * successive occurrences of a name are ignored. The presence of * unrecognized warning names is not an error: Compilers must * ignore any warning names they do not recognize. They are, however, * free to emit a warning if an annotation contains an unrecognized * warning name. * *
The string {@code "unchecked"} is used to suppress * unchecked warnings. Compiler vendors should document the * additional warning names they support in conjunction with this * annotation type. They are encouraged to cooperate to ensure * that the same names work across multiple compilers. * @return the set of warnings to be suppressed */ String[] value(); }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71478.html
摘要:中的詳解必修個多線程問題總結(jié)個多線程問題總結(jié)有哪些源代碼看了后讓你收獲很多,代碼思維和能力有較大的提升有哪些源代碼看了后讓你收獲很多,代碼思維和能力有較大的提升開源的運(yùn)行原理從虛擬機(jī)工作流程看運(yùn)行原理。 自己實(shí)現(xiàn)集合框架 (三): 單鏈表的實(shí)現(xiàn) 自己實(shí)現(xiàn)集合框架 (三): 單鏈表的實(shí)現(xiàn) 基于 POI 封裝 ExcelUtil 精簡的 Excel 導(dǎo)入導(dǎo)出 由于 poi 本身只是針對于 ...
摘要:注解有以下幾個知識點(diǎn)元數(shù)據(jù)注解的分類內(nèi)置注解自定義注解注解處理器本文先介紹前面?zhèn)€知識點(diǎn)元數(shù)據(jù)注解的分類內(nèi)置注解自定義注解。注解相當(dāng)于是一種嵌入在程序中的元數(shù)據(jù),可以使用注解解析工具或編譯器對其進(jìn)行解析,也可以指定注解在編譯期或運(yùn)行期有效。 大家好,我是樂字節(jié)的小樂,上次說過了Java多態(tài)的6大特性|樂字節(jié),接下來我們來看看Java編程里的注解。showImg(https://segme...
摘要:知識點(diǎn)總結(jié)注解解析注解知識點(diǎn)總結(jié)注解通過反射獲取類函數(shù)或成員上的運(yùn)行時注解信息,從而實(shí)現(xiàn)動態(tài)控制程序運(yùn)行的邏輯。 Java知識點(diǎn)總結(jié)(注解-解析注解) @(Java知識點(diǎn)總結(jié))[Java, 注解] 通過反射獲取類、函數(shù)或成員上的運(yùn)行時注解信息,從而實(shí)現(xiàn)動態(tài)控制程序運(yùn)行的邏輯。 使用注解步驟: 定義注解 類中使用注解 解析注解 示例: import java.lang.annotat...
摘要:編程思想第版這本書要常讀,初學(xué)者可以快速概覽,中等程序員可以深入看看,老鳥還可以用之回顧的體系。以下視頻整理自慕課網(wǎng)工程師路徑相關(guān)免費(fèi)課程。 我自己總結(jié)的Java學(xué)習(xí)的系統(tǒng)知識點(diǎn)以及面試問題,目前已經(jīng)開源,會一直完善下去,歡迎建議和指導(dǎo)歡迎Star: https://github.com/Snailclimb/Java-Guide 筆者建議初學(xué)者學(xué)習(xí)Java的方式:看書+視頻+實(shí)踐(初...
摘要:我們定義注解元素時,經(jīng)常使用空字符串作為默認(rèn)值。也經(jīng)常使用負(fù)數(shù)比如表示不存在的含義示例既可以修飾方法,也可以修飾類運(yùn)行時使用關(guān)鍵字定義注解成員以無參無異常方式聲明。方法的名稱就是參數(shù)的名稱可以使用為成員指定一個默認(rèn)值浙江大學(xué)清華大學(xué)張三 Java知識點(diǎn)總結(jié)(注解-自定義注解) @(Java知識點(diǎn)總結(jié))[Java, 注解] 使用@interface自定義注解時,自動繼承了java.lan...
閱讀 4041·2021-11-22 13:53
閱讀 3637·2021-11-19 11:29
閱讀 1296·2021-09-08 09:35
閱讀 3182·2020-12-03 17:26
閱讀 523·2019-08-29 16:06
閱讀 2121·2019-08-26 13:50
閱讀 1195·2019-08-23 18:32
閱讀 2165·2019-08-23 18:12