在java中@符号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31822020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in java what does the @ symbol mean?
提问by ViceVersa666444
I know what it means in a comment for documentation purposes, but outside of that what does it mean? (I would normally just google this but every non letter symbol shows up in results)
我知道出于文档目的的评论中的含义,但除此之外还有什么含义?(我通常只会谷歌这个,但每个非字母符号都会出现在结果中)
采纳答案by Sweeper
The @
symbol denotes a Java Annotation. What a Java annotation does, is that it adds a special attribute to the variable, method, class, interface, or other language elements. (This can be configured when you declare the annotation) When you add an annotation to something, other parts of the program can check whether something has an annotation or not. It then can use this information to do whatever stuff they need.
该@
符号表示的Java注释。Java 注释的作用是为变量、方法、类、接口或其他语言元素添加特殊属性。(这个可以在声明注解的时候配置)当你给某物添加注解时,程序的其他部分可以检查某物是否有注解。然后它可以使用这些信息来做他们需要的任何事情。
Let me give you some examples:
让我给你举几个例子:
The @Override
annotation
该@Override
注解
public class SuperClass {
public void someInterestingMethod() {
System.out.println("Superclass!");
}
}
public class DerivedClass extends SuperClass {
public void someInterestngMethod() {
System.out.println("Derived class!");
}
}
And when you do this:
当你这样做时:
SuperClass sc = new DerivedClass();
sc.someInterestingMethod();
The someInterestingMethod()
call should be dynamically dispatched, and print "Dervied class!"
, right? Well the derived class' method was actually misspelled, so DerivedClass
got its own separate method called someInterestngMethod()
, totally unrelated to the superclass' someInterestingMethod()
. As such, someInterestingMethod()
is no longer overridden, and the superclass' implementation is invoked.
该someInterestingMethod()
呼叫应被动态调度和打印"Dervied class!"
,对不对?好吧,派生类的方法实际上拼写错误,因此DerivedClass
有自己的单独方法称为someInterestngMethod()
,与超类完全无关someInterestingMethod()
。因此,someInterestingMethod()
不再被覆盖,并调用超类的实现。
The @Override
keyword is intended to help with this. It signals your intent to the compiler, that you would like the annotated method to be an overload of one of the ancestor class' methods. If it's not (such as in this typo case, or if the SuperClass
API changed and renamed the method), the will fail your compilation, to alert your attention to the broken override.
该@Override
关键字旨在帮助解决此问题。它向编译器表明您的意图,您希望带注释的方法是祖先类方法之一的重载。如果不是(例如在这种拼写错误的情况下,或者如果SuperClass
API 更改并重命名了方法),则编译将失败,以提醒您注意损坏的覆盖。
The @SuppressWarnings
Annotation
该@SuppressWarnings
注解
Here is a method:
这是一个方法:
public void someMethod() {
int i;
}
There will be a compiler warning saying that i
is never used. So you can add the @SuppressWarnings
to the method to suppressthe warning:
将有一个编译器警告说i
从未使用过。所以你可以添加@SuppressWarnings
到方法来抑制警告:
@SuppressWarnings("unused")
public void someMethod() {
int i;
}
Note that there is a parameter to the @SuppressWarnings
annotation. Some annotations have parameters and you can look for the them in the javadoc. But for those that don't have parameters you don't need to add ()
like a method.
请注意,注释有一个参数@SuppressWarnings
。一些注解有参数,您可以在 javadoc 中查找它们。但是对于那些没有参数的人,您不需要()
像方法一样添加。
You can also declare your own annotations and use reflection to check for them. The above 2 annotations will be checked by the compiler.
您还可以声明自己的注释并使用反射来检查它们。编译器会检查以上 2 个注释。
回答by Charles Spencer
The @ symbol is used for annotations. In my experience, the most common annotation is @Override
, which indicates that a method is declared in a superclass. Other common annotations are @Deprecated
, indicating that a method should no longer be used but still exists for backwards compatibility, and @SupressWarnings
, to prevent warnings from showing up in the compiler.
@ 符号用于注释。根据我的经验,最常见的注解是@Override
,它表示在超类中声明了一个方法。其他常见的注释是@Deprecated
,指示不应再使用某个方法,但为了向后兼容而仍然存在,以及@SupressWarnings
,以防止在编译器中出现警告。
Note that it's actually possible to get annotations which are not included in the core Java libraries and to declare your own annotations.
请注意,实际上可以获取未包含在核心 Java 库中的注释并声明您自己的注释。
回答by Abelard Chow
The @ sign is used to specify Java Annotation.
@ 符号用于指定 Java Annotation。
https://en.wikipedia.org/wiki/Java_annotation
https://en.wikipedia.org/wiki/Java_annotation
There are built-in Java Annotation and user defined Custom Annotation.
有内置的 Java Annotation 和用户定义的 Custom Annotation。
Annotations are used in various ways, such as suppress warning, associate method to URI (Servlet), associate variables to resource (JNDI) etc
注释的使用方式多种多样,例如抑制警告、将方法关联到 URI(Servlet)、将变量关联到资源(JNDI)等
回答by mr5
As some other suggests, it is Java's annotation. It helps the compiler to validate your code and to notify the programmer as well.
正如其他人所暗示的那样,它是 Java 的注释。它可以帮助编译器验证您的代码并通知程序员。
Very simple code example:
非常简单的代码示例:
public class SomeClass {
@Override
public String toString() {
return "SomeClass";
}
@Deprecated
public void doSomeOperation() {
// some operation...
}
}
The annotation from SomeClass#toString
which is @Override
helps the compiler to determine that it is an overridden function from the implicit inheritance to the class Object
.
注释来自SomeClass#toString
这@Override
有助于编译器,以确定它是从隐含的继承类的重载函数Object
。
While the annotation from SomeClass#doSomeOperation
will warn the programmer that the function itself is deprecated already and should be avoided to use.
虽然注释 fromSomeClass#doSomeOperation
将警告程序员该函数本身已被弃用,应避免使用。
回答by phydroxide
The annotations are for the reader or compiler, not executable code.
注释是给读者或编译器的,而不是可执行代码。
回答by Er. Mukesh Sharma
The @ symbol denotes Annotations. They provide information about a class, its field or method (above which they appear). They cannot perform operations. The compilers or special annotation processors use this information to make writing code less verbose.
@ 符号表示注解。它们提供有关类、其字段或方法(它们出现在其上方)的信息。他们无法执行操作。编译器或特殊注释处理器使用此信息来减少编写代码的冗长。
In Java Persistence API you use them to map a Java class with database tables.
在 Java Persistence API 中,您可以使用它们将 Java 类与数据库表进行映射。
For example @Table() Used to map the particular Java class to the date base table.
例如@Table() 用于将特定Java 类映射到日期基表。
@Entity Represents that the class is an entity class.
@Entity 表示该类是实体类。
Similarly you can use many annotations to map individual columns, generate ids, generate version, relationships etc.
同样,您可以使用许多注释来映射单个列、生成 id、生成版本、关系等。