Java中注解的目的是什么? [英] What is the purpose of annotations in Java?

查看:26
本文介绍了Java中注解的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解注解的目的是修改代码而不是实际代码,例如:

I understand that annotations serve a purpose to modify code without actually BEING code, such as:

@Author(
   name = "Benjamin Franklin",
   date = "3/27/2003"
)

但我不明白如何使用注释比只说 name = "Benjamin Franklin" 更好/更清晰/更简洁?添加注解如何加强代码?

But I don't understand how using the annotation is any better/ clearer/ more concise than just saying name = "Benjamin Franklin" ? How does the addition of annotations strengthen the code?

对不起,另一个问题,但我知道@Override 可以帮助防止/跟踪调用方法或类时的拼写错误,但它是如何做到的?它对实际程序有帮助吗?

Sorry for another questoin, but I know that @Override can help prevent/ track spelling mistakes when calling methods or classes, but how does it do that? Does it help the actual program at all?

推荐答案

注解是元数据.@Override 注释用于确保您覆盖超类的方法,而不仅仅是创建具有相同名称的方法.这里的常见错误包括:

Annotations are metadata. @Override annotation is used to make sure that you are overriding method of a superclass and not just making a method with the same name. Common mistakes here consist of:

  • 方法名拼写错误equal(Object o) 而不是 equals(Object o)
  • 输入不同的参数集

  • spelling the method's name wrong equal(Object o) instead of equals(Object o)
  • putting different set of arguments

MyString extends String { public boolean equals(MyString str) {} }

MyString extends String { public boolean equals(MyString str) {} }

equals(MyString str) 不会覆盖方法 equals(Object o),因此不会被标准 Java 比较器使用(在某些标准函数中使用),例如 List.contains() 并且这很容易出现错误情况).此注释有助于编译器确保您正确编码所有内容,从而有助于编程.

equals(MyString str) is not overriding the method equals(Object o) and therefore will not be used by standard Java comparators (which is used in some standard functions, such as List.contains() and this is prone to error situation). This annotation helps compiler to ensure that you code everything correctly and in this way it helps program.

@Deprecated 注释不会使程序无法编译,但它使开发人员考虑使用可以和/或将在未来版本中删除的代码.所以他们(开发人员)会考虑转移到另一个(更新的)功能集.如果您使用标记 -Xlint 编译您的程序,编译过程将返回错误,除非您删除所有弃用代码的用法或使用注释显式标记它们 @SuppressWarnings("deprecation").

@Deprecated annotation doesn't make program not to compile but it makes developers think about using the code that can and/or will be removed in a future releases. So they (developers) would think about moving onto another (updated) set of functions. And if you compile your program with the flag -Xlint compilation process will return with an error unless you remove all usages of deprecated code or explicitly mark them with annotation @SuppressWarnings("deprecation").

@SuppressWarnings 用于抑制警告(是的,我知道这是上尉明显的风格:)).@SuppressWarnings("deprecation") 有一个弃用抑制,@SuppressWarnings("unchecked") 和其他一些不安全的类型转换.当您的项目编译器具有编译标志 -Xlint 并且您不能(或不想)更改它时,这会很有帮助.

@SuppressWarnings is used to suppress warnings (yes, I know it's Captain Obvious style :)). There is a deprecation suppression with @SuppressWarnings("deprecation"), unsafe type casting with @SuppressWarnings("unchecked") and some others. This is helpfull when your project compiler have a compilation flag -Xlint and you cannot (or don't want to) change that.

您还可以将注释处理器集成到程序构建过程中,以确保程序代码符合某种标准.例如,对于 IntelliJ Idea IDE 注释处理器,您可以使用 @Nullable@NotNull 注释.当其他程序员使用您的代码时,它们会向其他程序员展示,以便可以将 null 作为某个参数传递给方法或不传递给方法.如果他们传递 null 将在编译期间或执行单行方法代码之前引发异常.

There are also annotation processors that you integrate into your program build process to ensure that program code meets some sort of criteria. For example with IntelliJ Idea IDE annotation processor you can use @Nullable and @NotNull annotations. They show other programmers when they use your code so that can transfer null as a certain parameter to a method or not. If they transfer null it will cause exception during compilation or before executing a single line method's code.

因此,如果您充分利用注释,注释将非常有用.

So annotations are quite helpful if you use them to their full potential.

这篇关于Java中注解的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆