展开全部

annotation。

Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰32313133353236313431303231363533e58685e5aeb931333264633435的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program's declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments: JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author("MyName")class myClass() { }

or @SuppressWarnings("unchecked")void MyMethod() { }

Defining your own annotation is an advanced technique that won't be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:

定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List;

class Food {}

class Hay extends Food {}

class Animal {

Food getPreferredFood() {

return null; } /** * @deprecated document why the method was deprecated */

@Deprecated

static void deprecatedMethod() { }

}

class Horse extends Animal {

Horse() {

return;

}

@Override

Hay getPreferredFood() {

return new Hay();

}

@SuppressWarnings("deprecation")

void useDeprecatedMethod() {

Animal.deprecateMethod(); //deprecation warning - suppressed }}

}

}

@DeprecatedThe @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.

@Deprecated@Deprecated annotation标注一个method不再被使用。编译器在一个program(程序?)使用了不赞成的方法,类,变量的时候会产生警告(warning)。如果一个元素(element:method, class, or variable)不赞成被使用,应该像前面的例子里使用相应的@deprecated 标签,并且注意标签的首字母是小写的"d",而annotation时大写的"D"。一般情况下,我们应该避免使用不赞成使用的方法(deprecated methods),而应该考虑替代的方法。

@OverrideThe @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error. While it's not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods.

@Override@Override annotation 告诉编译器当前元素是重写(override)自父类的一个元素。在前面的例子中,override annotation用来说明Horse类中的getPreferredFood这个方法重写(override)自Animal类中相同的方法。如果一个方法被标注了@Override,但是其父类中没有这个方法时,编译器将会报错。但是并不是说我们一定要使用这个annotation,但是它能够很明显的给出实际行为,尤其是在方法返回一个被重写的方法返回类型的子类型的时候。上面的例子中,Animal.getPreferredFood 返回一个 Food实例,Horse.getPreferredFood 返回一个Hay实例,这里Horse是Animal的子类,Hay是Food的子类。

@SuppressWarningsThe @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed. Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax: @SuppressWarnings@SuppressWarnings annotation 告诉编译器禁止别的元素产生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod调用了Animal的不赞成使用的一个方法。一般情况下,编译器会给出一个警告(warning),但是在这种情况下,不会产生这个警告,也就是说被suppress。每个编译器的警告都属于一个类型。Java Language Specification列出了两种类型:"deprecation" 和 "unchecked"。"unchecked" warning 发生在使用非generic的旧代码交互的generic collection类时。为了禁止不止一种的警告时,使用下面的语法:@SuppressWarnings({"unchecked", "deprecation"})

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

java元素符号是什么_Java 代码中 @ 符号是什么意思?相关推荐

  1. java 不要使用魔法值_Java代码中的魔法值

    所谓魔法值,是指在代码中直接出现的数值,只有在这个数值记述的那部分代码中才能明确了解其含义. 1. 前言 重构老代码中遇到了不少类似下面这种写法: public void attend(String ...

  2. java异常统一处理_Java 代码中的全局异常处理

    最近接手一个新项目,写用例的时候去翻看代码,发现部分代码里缺少基本的异常处理,包括对参数异常以及业务异常的处理.对照之前负责过的异常处理做得比较好的项目,给开发提了几点建议,顺便又去翻看了之前项目的代 ...

  3. java程序优化快捷键_Java 代码中针对性能优化的总结方案

    在一个好的项目中代码优化是永远离不开的话题,如果有充足的时间开发和维护代码,必须考虑每个可以优化的细节,日积月累,项目的质量才会上升,代码优化是绝对有必要的工作.代码优化的主要目的是减少代码块,提升代 ...

  4. java ip 白名单_Java代码中对IP进行白名单验证

    public classipUtil {//IP的正则,这个正则不能验证第一组数字为0的情况//private static Pattern pattern = Pattern//.compile(& ...

  5. java ip加密如何访问_java代码中如何实现http访问

    作者:释怀Na 出自:CSDN 原文:blog.csdn.net/qq_35906921/article/details/98076258 java代码中如何实现http访问 大概的实现就是 请求ht ...

  6. java输出日志_java代码中如何正确使用loggger日志输出

    java代码中如何正确使用loggger日志输出 发布时间:2019-06-28作者:spider阅读(2980) 当你遇到问题的时候,只能通过debug功能来确定问题,你应该考虑打日志,良好的系统, ...

  7. java 魔法值_Java代码中的魔法值

    所谓魔法值,是指在代码中直接出现的数值,只有在这个数值记述的那部分代码中才能明确了解其含义. 1. 前言 重构老代码中遇到了不少类似下面这种写法: public void attend(String ...

  8. java的注释规范_Java代码注释规范

    1,单行(单行)-简短说明: ///... 单行注释: 代码中的单行注释. 最好在注释前有一个空行,并在其后加上与代码相同的缩进级别. 如果无法完成一行,则应使用块注释. 评论格式: 在行首注释: 在 ...

  9. Java面试没过_Java面试中遇到的坑【填坑篇】

    1.StringBuilder替代String拼接,面试中经常会问到 String,StringBuilder,StringBuffer的区别. 解答:String类作为java语言中最常见的字符串类 ...

最新文章

  1. Java IO 总结图
  2. Using AVStream.codec.time_base as a timebase hint to the muxer is deprecated
  3. HTML5入门之样板和兼容IE浏览器篇
  4. windows 下远程登录ubuntu服务器--realVNC
  5. 从Spark Streaming到Apache Flink: 实时数据流在爱奇艺的演进 | 技术头条
  6. 【今日CV 视觉论文速览】 Part2 19 Feb 2019
  7. Apache Ant使用过程的总结
  8. php80端口改成8080,jetty,_JETTY 8080端口改为80端口无法访问,jetty - phpStudy
  9. 解决ERROR 1044 (42000): Access denied for user
  10. windows rt运行android,Move from Android to WinRT
  11. plsql保持长连接_知乎千万级高性能长连接网关是如何搭建的
  12. Namespace名称空间
  13. NCI和CNC应用中M代码的概念和功能
  14. 新书出版:《数字滤波器的MATLAB与FPGA实现——Altera/Verilog版(第2版)》已开始印刷出版
  15. 简单的个人介绍网页【附代码】
  16. 网易微专业IOS开发工程师教程(完整)
  17. 长芯微LCM1118 16 位模数转换器 P2P替代ADS1118
  18. Joplin插件推荐-持续更新
  19. CAN总线接口静电保护及ESD二极管选型
  20. 错误: 找不到符号 符号: 方法 getResources() 位置: 类型为String的变量 dataSex

热门文章

  1. 论文浅尝 - EMNLP2020 | 基于规则引导的协作 agent 知识图谱推理学习
  2. 百度开源 FAQ 问答系统(AnyQ)安装---Linux(无docker)+小白编译AnyQ-dockerlinux[CentOs]
  3. Android官方开发文档Training系列课程中文版:高效显示位图之在非UI线程中处理图片
  4. 笔记:python设计模式
  5. JAVA面试题:HashMap和Hashtable的区别
  6. 解决方案:c调用python,PyImport_Import或者PyImport_ImportModule总是返回为空
  7. Unity(四):使用场景Ⅰ:建立类型映射
  8. C++调用C#的dll
  9. 牛客16654 谁拿了最多奖学金
  10. 第二章 数据的表示和运算 2.1.1 进位计数制 [计算机组成原理笔记]