JDK中常见注解简介说明
下文笔者讲述JDK中几个常见注解的使用示例分享,如下所示
@SuppressWarnings({"unchecked", "deprecation"})注解
JDK中常用的几个注解如下所示: @Override注解 @Deprecated注解 @SuppressWarnings
@Override注解:功能为:重写
public class OverrideTest{ @Override public String toString(){ return "方法被重写!"; } public static void main(String[] args){ OverrideTest test = new OverrideTest(); System.out.println(test); } }
@Deprecated注解:标记方法过时(不建议被使用)
public class DeprecatedTest{ @Deprecated public void doSomething(){ System.out.println("方法被运行!"); } public static void main(String[] args){ DeprecatedTest test = new DeprecatedTest(); test.doSomething(); Date date = new Date(); System.out.println(date.toLocaleString()); } }
@SuppressWarnings({"unchecked", "deprecation"})注解
表示压制警告
import java.util.Date; import java.util.Map; import java.util.TreeMap; public class SuppressWarningsTest{ @SuppressWarnings({"unchecked", "deprecation"}) public static void main(String[] args){ Map map = new TreeMap(); map.put("hello", new Date()); System.out.println(map.get("hello")); Date date = new Date(); System.out.println(date.toLocaleString()); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。