Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Scala中定义@接口?_Scala_Annotations - Fatal编程技术网

如何在Scala中定义@接口?

如何在Scala中定义@接口?,scala,annotations,Scala,Annotations,如何在Scala中创建@接口?我真的觉得问这个问题很愚蠢,但是我在任何地方都找不到这个问题的语法。我知道你可以使用它们,但是你如何在Scala中定义新的呢 爪哇: 斯卡拉: ??? 这个答案基于Scala 2.8 // Will be visible to the Scala compiler, but not in the class file nor at runtime. // Like RetentionPolicy.SOURCE final class MyAnnotation ex

如何在Scala中创建
@接口
?我真的觉得问这个问题很愚蠢,但是我在任何地方都找不到这个问题的语法。我知道你可以使用它们,但是你如何在Scala中定义新的呢

爪哇:

斯卡拉:

???

这个答案基于Scala 2.8

// Will be visible to the Scala compiler, but not in the class file nor at runtime.
// Like RetentionPolicy.SOURCE
final class MyAnnotation extends StaticAnnotation

// Will be visible stored in the annotated class, but not retained at runtime.
// This is an implementation restriction, the design is supposed to support
// RetentionPolicy.RUNTIME
final class MyAnnotation extends ClassfileAnnotation
有关详细信息,请参阅中的第11节“用户定义的注释” 例如,请参见:

更新编译器警告最好说明:

>cat test.scala
final class MyAnnotation extends scala.ClassfileAnnotation

@MyAnnotation
class Bar

>scalac test.scala
test.scala:1: warning: implementation restriction: subclassing Classfile does not
make your annotation visible at runtime.  If that is what
you want, you must write the annotation class in Java.
final class MyAnnotation extends scala.ClassfileAnnotation

如果要在Scala中创建注释,应混合使用
StaticAnnotation
ClassAnnotation
特性。示例代码:

class MyBaseClass  {}  
class MyAnnotation(val p:String) extends MyBaseClass with StaticAnnotation  {}
@MyAnnotation("AAA")  
class MyClass{}  

虽然
StaticAnnotation
s允许我使用和编译它们,但如何在运行时查询它们?在Scala中可以找到标有
@Retention(RetentionPolicy.RUNTIME)
的Java注释,但是我找不到Scala中定义的
StaticAnnotation
s,尽管我正在使用运行时保留策略对它们进行标记。您或其他人能否给出一个示例,说明如何在Java中定义运行时注释并在Scala中使用它?Scala 2.9.1中仍然存在实现限制。我想知道这是否真的值得?注释类往往很小,所以在Scala中编写它们并没有真正的优势。就像枚举写入Java一样。如何获取注释中的参数define\d?就像这个例子中的参数值“AAA”???
class MyBaseClass  {}  
class MyAnnotation(val p:String) extends MyBaseClass with StaticAnnotation  {}
@MyAnnotation("AAA")  
class MyClass{}