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

展开全部

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"})

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/496478.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

风景这边独好的AI大年,百度亚马逊等巨头第一季度成绩单怎么样?

苹果春季新品发布会邀请函来源&#xff1a;遇见人工智能该来的还是来了&#xff0c;但是那又如何&#xff1f;这句话是送给苹果的。本以为这个季度不会再有新品发布的苹果&#xff0c;却用一张主打“同学们&#xff0c;来次课外活动吧”的神奇邀请函打消了所有人的怀疑。根据从…

最近火了的自动驾驶全球产业链全景图

来源&#xff1a;传感器技术摘要&#xff1a;目前自动驾驶已经发展的很快&#xff0c;除了特斯拉和奥迪以外&#xff0c;很多豪车都引进了自动驾驶技术。下面来盘点一下自动驾驶产业链&#xff1a;首先看一下自动驾驶的等级标准&#xff1a;1、英特尔&#xff1a;Mobileye Alt…

java get set 注解_java技能提升,用Lombok甩掉get和set,让代码变得更简洁

前言前几天有个新来的同事(实习生)惊讶的对我说&#xff1a;我们的代码里好多错误&#xff0c;我的程序本地都启动不了。我一脸懵逼的质问他&#xff1a;目前线上的代码&#xff0c;怎么会有问题吗&#xff1f;他不服气的说&#xff1a;你来看嘛&#xff0c;就是有问题&#xf…

.pfx 证书和 .cer 证书

证书系列&#xff1a; 1&#xff1a;.pfx 证书和 .cer 证书 2&#xff1a;导入pfx证书 通常情况下&#xff0c;作为文件形式存在的证书一般有三种格式&#xff1a; 第一种&#xff1a;带有私钥的证书&#xff0c;由Public Key Cryptography Standards #12&#xff0c;PKCS#1…

HTML5、CSS、CSS3、SCSS (SASS) 相关教程

1、HTML5 教程 W3School HTML5 教程&#xff1a;http://www.w3school.com.cn/html5/index.asp 菜鸟网站 HTML5教程&#xff1a;http://www.runoob.com/html/html5-intro.html 知乎 零基础如何迅速学习HTML&#xff1a;https://www.zhihu.com/question/27018083 请问如何从头…

生命起源之谜:RNA世界假说将迎来终结?

○ 流行的理论认为&#xff0c;生命起源于物质丰富的化学汤&#xff0c;而 RNA 是化学汤中最初的自我复制单元。但是&#xff0c;多肽和RNA混合起来或许会更高效。 | 图片来源&#xff1a;Novikov Aleksey来源&#xff1a;科学出版社 撰文&#xff1a;Jordana Cepelewicz 翻…

PHP 学习路线

PHP 官网文档(中文)&#xff1a;https://www.php.net/manual/zh/langref.php ThinkPhp (官方手册、入门教程)&#xff1a;https://sites.thinkphp.cn/1556331 ​W3School PHP 教程&#xff1a;http://www.w3school.com.cn/php/index.asp w3cschool (在线教程&技术文档)&am…

jQuery 对话框 jQuery.plugin

jQuery 对话框 jQuery.plugin 强烈推荐对话框插件jquery.weebox.js&#xff0c;本站开源账务管理系统中使用的对话框组件&#xff0c;各种形式的对话框&#xff1a;确认、成功、警告、错误等 ………… 如下图的右下角&#xff1a; 账务管理系统(个人版)演示 图的右下角的框架就…

2018年聊天机器人状态报告

来源&#xff1a; 199IT互联网数据中心根据Drift、SurveyMonkey Audience、Salesforce和myclever的“2018年聊天机器人状态报告”&#xff0c;聊天机器人预计能够24小时为简单任务提供即时服务&#xff0c;但不是进行复杂查询的最佳渠道。聊天机器人尚未在消费者中找到广泛的吸…

PHP、MySQL 注入

Welcome to the NetSPI SQL Injection Wiki&#xff1a;https://sqlwiki.netspi.com/ 因为需要了解下 SQL 注入&#xff0c;就使用 PHP 自己写了一个只有一个网页的网站测试下&#xff0c;现在记录下过程。。。 直接使用的 KALI系统 (KALI官网&#xff1a;Kali Linux | Penetr…

争自动驾驶领头羊还是确保技术安全?欧美选择不同

来源&#xff1a;发掘新视界摘要&#xff1a;对于那些未知或有潜在危险的技术&#xff0c;欧洲更倾向于保护民众&#xff0c;而非是引领创新与进步。自优步自动驾驶汽车致人死亡事件发生之后&#xff0c;欧洲与美国对于技术的态度差异再度凸显&#xff0c;欧洲更倾向于加强监管…

菜鸟教程 之 JavaScript 教程

From&#xff1a;菜鸟教程 JavaScript&#xff1a;https://www.runoob.com/ W3School JavaScript 教程&#xff1a;http://www.w3school.com.cn/js/index.asp https://www.w3cschool.cn/javascript/ 廖雪峰官网 JavaScript 教程&#xff1a;https://www.liaoxuefeng.com/w…

关于Actionscript 3中给Flash传参数方法(一)

关于Actionscript 3中给Flash传参数方法&#xff08;一&#xff09; 今天在测试一个为Audi做Flash AD的时候&#xff0c;发现Audi提供的clicktag是Actionscript 2的代码&#xff0c;在Actionscript 3中是不被支持的&#xff0c;所以有去翻了Adobe Docs了。 在2.0中的clicktag代…

深度睡眠中,记忆如何被保持?

来源 &#xff1a;脑与智能丨公众号摘要&#xff1a;深度睡眠对记忆的巩固至关重要&#xff0c;与健康的睡眠相比&#xff0c;失眠会导致记忆力下降。深度睡眠中的慢波分为下降状态&#xff08;down state&#xff09;和上升状态&#xff08;up state&#xff09;&#xff0c;在…

SQL 语句执行顺序

From&#xff1a;http://www.jellythink.com/archives/924 Oracle-SQL语句执行原理和完整过程详解&#xff1a;https://wenku.baidu.com/view/398bc427964bcf84b8d57b00.html 详解一条 SQL 语句的执行过程&#xff1a;http://www.cnblogs.com/cdf-opensource-007/p/6502556.…

无人车巨头每天都在做相同的事情:不惜血本做高精地图是为何?

来源&#xff1a;腾讯科技摘要&#xff1a;很多人都关注无人车的发展&#xff0c;殊不知无人车技术中&#xff0c;有十分重要的一环&#xff0c;是无人车的高精地图。高精地图成为无人车竞赛中的关键一环目前&#xff0c;很多家无人驾驶公司都在做着同样的事情&#xff1a;绘制…

phaser java_死磕 java同步系列之Phaser源码解析

问题(1)Phaser是什么&#xff1f;(2)Phaser具有哪些特性&#xff1f;(3)Phaser相对于CyclicBarrier和CountDownLatch的优势&#xff1f;简介Phaser&#xff0c;翻译为阶段&#xff0c;它适用于这样一种场景&#xff0c;一个大任务可以分为多个阶段完成&#xff0c;且每个阶段的…

人工智能将为传统制造业带来什么?

来源 &#xff1a;数据科学与人工智能“物理世界”&#xff08;以制造业设备所代表&#xff09;和“数字世界”&#xff08;由人工智能、传感器等技术代表&#xff09;的碰撞催生了制造业的巨大的转变。两个世界的融合将为下一轮经济发展注入新的动能。以人工智能为代表的新技术…

网络模拟器 eNSP、EVE-NG、GNS3、Packet Tracert

工欲善其事必先利其器&#xff0c;学习网络不可能都有真实的网络环境可以使用&#xff0c;这时就需要使用网络模拟软件&#xff0c;模拟一些网络环境&#xff0c;构建一些网络拓扑&#xff0c;然后学习研究网络技术 初学时不要在模拟器的选择上纠结&#xff0c;PT、GNS3 就足够…

信息技术守护人类文明DNA

来源&#xff1a;中国青年报 作者&#xff1a;胡春艳21世纪最时髦的技术&#xff0c;改变的不仅是未来&#xff0c;在被称为“最古老的研究”的文物与博物馆界&#xff0c;感受到的变化可能更加显著。天津大学文化遗产保护与传承信息技术研究中心主任张加万团队历时10年&…