java中的interface和@interface有什么区别?

新手上路,请多包涵

自从 90 年代后期在大学期间使用 JBuilder 后,我就没有接触过 Java,所以我有点脱节 - 无论如何,我本周一直在从事一个小型 Java 项目,并使用 Intellij IDEA 作为我的 IDE ,以改变我的常规 .Net 开发步伐。

我注意到它支持添加接口和@interface,什么是@interface,它与普通接口有什么不同?

 public interface Test {
}

对比

public @interface Test {
}

我进行了一些搜索,但找不到大量有关@interface 的有用信息。

原文由 Bittercoder 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 800
2 个回答

@ 符号表示注释类型定义。

这意味着它 并不是 真正的接口,而是一种新的注解类型——用作函数修饰符,例如 @override

请参阅有关该主题的此 javadocs 条目

原文由 mrkishi 发布,翻译遵循 CC BY-SA 2.5 许可协议

界面:

通常,接口公开契约而不公开底层实现细节。在面向对象编程中,接口定义公开行为但不包含逻辑的抽象类型。实现由实现接口的类或类型定义。

@interface : (注解类型)

以下面的例子为例,它有很多评论:

 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

}

取而代之的是,您可以声明一个注解类型

 @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();
}

然后可以按如下方式注释一个类:

 @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: 很多注解代替了代码中的注释。

参考: http ://docs.oracle.com/javase/tutorial/java/annotations/declaring.html

原文由 mavis 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题