當注解有一個基本型別如 String 或 Int 的陣列引數時,如何使用它很簡單:
public @interface MyAnnotation{
String[] props();
}
@MyAnnotation(props = ["A", "B", "C"])
class Foo {}
不幸的是,這不適用于本身就是注釋的值。
一個例子是org.springframework.context.annotation.PropertySources:
public @interface PropertySources {
PropertySource[] value();
}
public @interface PropertySource {
String[] value();
}
在 Java 語法中的用法是
@PropertySources({
@PropertySource({"A", "B", "C"}),
@PropertySource({"D", "E", "F"}),
})
class Foo{}
但是在 Kotlin 中,類似方法的代碼無法編譯
@PropertySources([
@PropertySource(["A", "B", "C"]),
@PropertySource(["D", "E", "F"]),
])
class Foo{}
如何在 Kotlin 中表達這個注解陣列嵌套構造?
uj5u.com熱心網友回復:
從子注釋宣告中添加value =和洗掉@:
@PropertySources(value = [
PropertySource("a", "b"),
PropertySource("d", "e"),
])
class Foo
另外請注意,@PropertySource是@Repeatable這樣,你可以這樣做:
@PropertySource("a", "b")
@PropertySource("d", "e")
class Foo
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/398995.html
上一篇:從FileProvider.getUriForFile()檢索的imageUri在imageView上設定但顯示空ImageView
下一篇:Kotlin多個when陳述句
