我想要定義Java代碼使用的特徵,因此爲其成員提供適用於Java的setter和getter會很方便。 @BeanProperty
註釋對我來說是這樣,但我無法讓它與在特徵中未定義的成員一起工作。以下:如何使用必須實現的@BeanProperty成員編寫特質?
import scala.beans.BeanProperty
trait A {
@BeanProperty var useFoo: Boolean
}
產生了警告no valid targets for annotation on method useFoo - it is discarded unused. You may specify targets with meta-annotations, e.g. @(scala.beans.BeanProperty @getter) trait A { @BeanProperty var useFoo: Boolean }
但是,它沒有丟棄不用。例如
class B extends A {
var useFoo = false
}
被編譯器正確拒絕,因爲它沒有實現BeanProperty的getter和setter。使用@BeanProperty
註解類B
中的useFoo
字段使其按預期工作。但是,這似乎不是正確的做法,因爲會產生上述警告。 The documentation for meta annotations表明目標只有在您想將其他註釋傳播到生成的訪問器時纔有用。那麼,定義上述特徵的正確方法是什麼?
fyi,方法/值的差異似乎源於'val'與'var',而不是從版本 – 2015-04-20 15:52:48