java中的接口和@interface有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/918393/
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
What's the difference between interface and @interface in java?
提问by Bittercoder
I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular .Net development.
自从上世纪 90 年代末在大学期间使用 JBuilder 以来,我一直没有接触过 Java,所以我有点脱节 - 无论如何,我本周一直在从事一个小型 Java 项目,并使用 Intellij IDEA 作为我的 IDE ,改变了我常规 .Net 开发的步伐。
I notice it has support for adding interfaces and @interfaces, what is an @interface, and how does it differ from a normal interface?
我注意到它支持添加接口和@interfaces,什么是@interface,它与普通接口有什么不同?
public interface Test {
}
vs.
对比
public @interface Test {
}
I've done a bit of searching, but couldn't find a great deal of useful info referring to @interface.
我进行了一些搜索,但找不到大量有关@interface 的有用信息。
采纳答案by mrkishi
The @symbol denotes an annotation type definition.
的@符号表示注解类型的定义。
That means it is notreally an interface, but rather a new annotation type -- to be used as a function modifier, such as @override.
这意味着它不是真正的接口,而是一种新的注释类型——用作函数修饰符,例如@override。
See this javadocs entryon the subject.
请参阅有关该主题的此javadocs 条目。
回答by DavidValeri
The interface
keyword indicates that you are declaring a traditional interface class in Java.
The @interface
keyword is used to declare a new annotation type.
该interface
关键字表示您在 Java 中声明了一个传统的接口类。
该@interface
关键字用于声明一个新的注释类型。
See docs.oracle tutorialon annotations for a description of the syntax.
See the JLSif you really want to get into the details of what @interface
means.
有关语法的说明,请参阅有关注释的docs.oracle 教程。如果您真的想详细了解什么意思,
请参阅JLS@interface
。
回答by mavis
interface:
界面:
In general, an interface exposes a contract without exposing the underlying implementation details. In Object Oriented Programming, interfaces define abstract types that expose behavior, but contain no logic. Implementation is defined by the class or type that implements the interface.
一般来说,一个接口暴露了一个契约而不暴露底层的实现细节。在面向对象编程中,接口定义了公开行为但不包含逻辑的抽象类型。实现由实现接口的类或类型定义。
@interface : (Annotation type)
@interface : (注解类型)
Take the below example, which has a lot of comments:
以下面的例子为例,它有很多评论:
public class Generation3List extends Generation2List {
// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy
// class code goes here
}
Instead of this, you can declare an annotation type
取而代之的是,您可以声明一个注释类型
@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
// Note use of array
String[] reviewers();
}
which can then annotate a class as follows:
然后可以注释一个类,如下所示:
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
PS: Many annotations replace comments in code.
PS: 很多注解取代了代码中的注释。
Reference: http://docs.oracle.com/javase/tutorial/java/annotations/declaring.html
参考:http: //docs.oracle.com/javase/tutorial/java/annotations/declaring.html
回答by Emmanuel Osimosu
interface:
defines the contract for a class which implements it
interface:
定义实现它的类的契约
@interface:
defines the contract for an annotation
@interface:
定义注释的契约
回答by Piyush Singh
interfacein the Java programming language is an abstract type that is used to specify a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the interface keyword
Java 编程语言中的接口是一种抽象类型,用于指定类必须实现的行为。它们类似于协议。接口使用 interface 关键字声明
@interfaceis used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example:
@interface用于创建您自己的(自定义)Java 注释。注释在它们自己的文件中定义,就像 Java 类或接口一样。这是自定义 Java 注释示例:
@interface MyAnnotation {
String value();
String name();
int age();
String[] newNames();
}
This example defines an annotation called MyAnnotation which has four elements. Notice the @interface keyword. This signals to the Java compiler that this is a Java annotation definition.
这个例子定义了一个名为 MyAnnotation 的注解,它有四个元素。注意@interface 关键字。这向 Java 编译器发出信号,表明这是一个 Java 注释定义。
Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name. You can use all primitive data types as element data types. You can also use arrays as data type. You cannot use complex objects as data type.
请注意,每个元素的定义类似于接口中的方法定义。它有一个数据类型和一个名称。您可以将所有原始数据类型用作元素数据类型。您还可以使用数组作为数据类型。您不能使用复杂对象作为数据类型。
To use the above annotation, you could use code like this:
要使用上述注释,您可以使用如下代码:
@MyAnnotation(
value="123",
name="Jakob",
age=37,
newNames={"Jenkov", "Peterson"}
)
public class MyClass {
}
Reference - http://tutorials.jenkov.com/java/annotations.html