Classes · 34 of 42
var name to a method def name you will not need to recompile the codedef, val or var, private or public) that has the same name in a class@BeanProperty annotation to instruct the compiler to automatically add a Java bean style getter and setter.getName()/setName(...) exist for Java callers; Scala code keeps using the uniform accessors (sp.name)isFlag use @BooleanBeanProperty instead//A full Java boilerplate style class (not idiomatic Scala!)
class JPerson() {
var _name: String = null
def this(_name:String) = {
this()
this._name = _name
}
//Scala style getters and setters
def name_=(_name:String) = this._name = _name
def name = this._name
//Java style getters and setters
def getName() = name
def setName(name:String) = this.name = name
}
//Which can be generated in 1 line of idiomatic Scala
import scala.beans.*
class SPerson(@BeanProperty var name:String)
//Note: @BeanProperty is optional
//(only if you need Java code to call getName()/setName:
// the generated bean methods are for Java interop; from
// Scala you use the uniform accessors below)
val jp = new JPerson("Java Style")
val sp = new SPerson("Scala Style")
println(jp.name)
println(sp.name)
jp.name += " sucks!"
sp.name += " rocks!"
println(jp.getName())
println(sp.name)