當將scala字段標準爲@BeanProperty時,會自動生成get/set方法。java
import scala.reflect.BeanProperty class Counter_3 { @BeanProperty var name: String = "hello" } object Counter_3 { def main(args: Array[String]) { var counter_3 = new Counter_3 counter_3.setName("chen") print(counter_3.getName) counter_3.name println() counter_3.name = "skdkd" counter_3.name_= ("sdsd") println(counter_3.name) } }
結果ide
chen sdsd
將會生成四個方法spa
name:Stringscala
name_=(newValue: String): Unitci
getName(): Stringget
setName(newValue: String): Unitit
若是以主構造器參數的方式定義了某字段,而且你須要javabean版的getter和setter方法。class
import scala.reflect.BeanProperty class Counter_3(@BeanProperty var name: String) { // @BeanProperty // var name: String = "hello" } object Counter_3 { def main(args: Array[String]) { var counter_3 = new Counter_3("skdkds") counter_3.setName("chen") print(counter_3.getName) counter_3.name println() counter_3.name = "skdkd" counter_3.name_=("sdsd") println(counter_3.name) } }結果:
chen sdsd