在scala2.10 版本,可以通过导入import scala.reflect.BeanProperty包,在field声明,但是2.11版本这样就不行用了找不到这个包

scala 2.11 版本自动生成setter getter方法

 查看官方API,我们可以导入scala.beans.BooleanBeanProperty:

https://www.scala-lang.org/api/2.11.5/index.html#scala.beans.BeanProperty

scala 2.11 版本自动生成setter getter方法

This annotation has the same functionality as scala.beans.BeanProperty, but the generated Bean getter will be named isFieldName instead of getFieldName.:意思就是我们可以通过导入scala.beans.BooleanBeanProperty包实现同样的方法,但是以前的getFieldName方法,也就是我们的getter方法,用isFieldName替代了

测试如下,编写Students类:

import scala.beans.BooleanBeanProperty
class Students {
  @BooleanBeanProperty
  var name1: String ="test"
}

调用getter (isFieldName) & setter方法:

object testPrint {
  def main(args: Array[String]): Unit = {
    val s =new Students
    println(s.isName1)
    s.setName1("test222")
    println(s.isName1)
  }
}

测试结果输出:

scala 2.11 版本自动生成setter getter方法


相关文章:

  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2021-06-28
  • 2021-04-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-18
  • 2021-08-01
  • 2022-12-23
  • 2021-05-30
  • 2021-08-01
  • 2021-10-08
相关资源
相似解决方案