gpt4 book ai didi

java - 将数组传递给注释的语法

转载 作者:行者123 更新时间:2023-12-01 12:45:23 26 4
gpt4 key购买 nike

几天前,我被迫在我的类声明中使用以下结构:

@Table(name="UserPattern",  uniqueConstraints={
@UniqueConstraint(columnNames={"user_id", "patern_id"})
})

我对这种语法感到非常惊讶。

通常我认为如果我应该将数组传递给注释 O 我应该编写以下内容:

declared_inside_annotation_name={value1,value2...}

但在本例中,它看起来如下所示:

uniqueConstraints={
@UniqueConstraint(columnNames={"user_id", "patern_id"})
}

@Table注释声明:

@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {

String name() default "";


String catalog() default "";


String schema() default "";


UniqueConstraint[] uniqueConstraints() default { };


Index[] indexes() default {};
}

请澄清此语法。

最佳答案

您的 declared_inside_annotation_name 示例中的预期语法与 @Table 注释中的语法实际上并不冲突。注释的数组属性的元素类型不一定必须是字符串(这可能是您所期望的)。它实际上可能是另一个注释。

@Table 注释的 uniqueConstraints 属性就是这种情况。如果您检查 UniqueConstraint 类的声明,您会发现它本身就是一个注释。写下来时,您可以使用常用的 @AnnotationTypeName 表示法。

关于java - 将数组传递给注释的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24754451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com