资讯专栏INFORMATION COLUMN

Java知识点总结(注解-内置注解)

J4ck_Chan / 2881人阅读

摘要:知识点总结注解内置注解知识点总结注解定义在中,此注释只适用于修饰方法,表示一个方法声明打算重写父类的另一个方法声明。此注释可用于修饰方法属性类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。

Java知识点总结(注解-内置注解)

@(Java知识点总结)[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:
 *
 * 
  • * The method does override or implement a method declared in a * supertype. *
  • * The method has a signature that is override-equivalent to that of * any public method declared in {@linkplain Object}. *
* * @author Peter von der Ahé * @author Joshua Bloch * @jls 9.6.1.4 @Override * @since 1.5 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.SOURCE) public @interface Override { }
@Deprecated

定义在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中,用来抑制编译时的警告信息。

与前两个注释有所不同, 你需要添加一个参数才能正确使用 ,这些参数值是已经定义好了的,我们选择性的使用就好了,参数如下:

参数 说明
deprecation 使用了过时的类或方法的警告
unchecked 执行了未检查的转换时的警告,如使用集合时未指定泛型
fallthrough 当在switch语句使用时发生case穿透
path 在类路径、源文件路径等中有不存在路径的警告
serial 当在可序列化的类上缺少serialVersionUID定义时的警告
finally 任何finally子句不能完成时的警告
all 关于以上所有情况的警告
@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(); }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71478.html

相关文章

  • Java 总结

    摘要:中的详解必修个多线程问题总结个多线程问题总结有哪些源代码看了后让你收获很多,代码思维和能力有较大的提升有哪些源代码看了后让你收获很多,代码思维和能力有较大的提升开源的运行原理从虚拟机工作流程看运行原理。 自己实现集合框架 (三): 单链表的实现 自己实现集合框架 (三): 单链表的实现 基于 POI 封装 ExcelUtil 精简的 Excel 导入导出 由于 poi 本身只是针对于 ...

    caspar 评论0 收藏0
  • Java注解-元数据、注解分类、内置注解和自定义注解

    摘要:注解有以下几个知识点元数据注解的分类内置注解自定义注解注解处理器本文先介绍前面个知识点元数据注解的分类内置注解自定义注解。注解相当于是一种嵌入在程序中的元数据,可以使用注解解析工具或编译器对其进行解析,也可以指定注解在编译期或运行期有效。 大家好,我是乐字节的小乐,上次说过了Java多态的6大特性|乐字节,接下来我们来看看Java编程里的注解。showImg(https://segme...

    Yujiaao 评论0 收藏0
  • Java识点总结注解-解析注解

    摘要:知识点总结注解解析注解知识点总结注解通过反射获取类函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。 Java知识点总结(注解-解析注解) @(Java知识点总结)[Java, 注解] 通过反射获取类、函数或成员上的运行时注解信息,从而实现动态控制程序运行的逻辑。 使用注解步骤: 定义注解 类中使用注解 解析注解 示例: import java.lang.annotat...

    awkj 评论0 收藏0
  • 一份送给Java初学者的指南

    摘要:编程思想第版这本书要常读,初学者可以快速概览,中等程序员可以深入看看,老鸟还可以用之回顾的体系。以下视频整理自慕课网工程师路径相关免费课程。 我自己总结的Java学习的系统知识点以及面试问题,目前已经开源,会一直完善下去,欢迎建议和指导欢迎Star: https://github.com/Snailclimb/Java-Guide 笔者建议初学者学习Java的方式:看书+视频+实践(初...

    banana_pi 评论0 收藏0
  • Java识点总结注解-自定义注解

    摘要:我们定义注解元素时,经常使用空字符串作为默认值。也经常使用负数比如表示不存在的含义示例既可以修饰方法,也可以修饰类运行时使用关键字定义注解成员以无参无异常方式声明。方法的名称就是参数的名称可以使用为成员指定一个默认值浙江大学清华大学张三 Java知识点总结(注解-自定义注解) @(Java知识点总结)[Java, 注解] 使用@interface自定义注解时,自动继承了java.lan...

    zhouzhou 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<