1、次级构造器(辅助构造器)
case class Address(street: String, city: String, state: String, zip: String){ //伴生类
// 次级构造函数
def this(zip: String) =
this("[unknown]", Address.zipToCity(zip), Address.zipToState(zip), zip) //"="后面的this构造函数为主构造函数,这里没有显式
//地写出来
}
object Address { //伴生对象
def zipToCity(zip: String) = "Anytown";
def zipToState(zip: String) = "CA";
}
case class Person(name: String, age: Option[Int], address: Option[Address]){
// 下面4个次级构造器相互之间为重载的关系
def this(name: String ) = this(name, None, None ); // this1
def this(name: String, age: Int ) = this(name, Some(age), None ); // this2
def this(name: String, age: Int, address: Address) = this(name, Some(age), Some(address)); // this3
def this(name: String, address: Address) = this(name, None, Some(address)); // this4
}
object Test{
def main(args: Array[String]){
val a1 = new Address("1 Scala Lane", "Anytown", "CA", "98765"); // 调用主构造函数
val a2 = new Address("98765"); // 调用次级构造函数
val p1 = new Person("Buck Trend1"); // 调用this1
val p2 = new Person("Buck Trend2", Some(20), Some(a1)); // 调用this3
val p2_1 = Person("Buck Trend2", Some(20), Some(a1)); // 调用主构造器
val p3 = new Person("Buck Trend3", 20, a2 ); // 调用this3
val p4 = new Person("Buck Trend4", 20 ); // 调用this2
println(a1);
println(a2);
println(p1);
println(p2);
println(p2_1);
println(p3);
println(p4);
}
}
// 运行结果
Person(Buck Trend1,None,None)
Person(Buck Trend2,Some(20),Some(Address(1 Scala Lane,Anytown,CA,98765)))
Person(Buck Trend2,Some(20),Some(Address(1 Scala Lane,Anytown,CA,98765)))
Person(Buck Trend3,Some(20),Some(Address([unknown],Anytown,CA,98765)))
Person(Buck Trend4,Some(20),None)
2. 对构造器参数使用默认值(这样可以避免像上面那样单独构造很多辅助构造器)
如果对参数使用默认值,则创建对象时,参数必须使用Some(...)
case class Address(street: String, city: String, state: String, zip: String){ //伴生类
// 次级构造函数
def this(zip: String) =
this("[unknown]", Address.zipToCity(zip), Address.zipToState(zip), zip) //"="后面的this构造函数为主构造函数,这里没有显式
//地写出来
}
object Address { //伴生对象
def zipToCity(zip: String) = "Anytown";
def zipToState(zip: String) = "CA";
}
case class Person2(
name: String,
age: Option[Int] = None,
address: Option[Address] = None);
object Test{
def main(args: Array[String]){
val a1 = new Address("1 Scala Lane", "Anytown", "CA", "98765"); // 调用主构造函数
val a2 = new Address("98765"); // 调用次级构造函数
val p1 = new Person2("Buck Trend1"); // 调用this1
val p2 = new Person2("Buck Trend2", Some(20), Some(a1)); // 调用this3
val p2_1 = Person2("Buck Trend2", Some(20), Some(a1)); // 调用主构造器
// val p3 = new Person2("Buck Trend3", 20 ); // 报错: required: Option[Int]
// val p4 = new Person2("Buck Trend4", address = a2 ); // 报错: required: Option[Address]
println(a1);
println(a2);
println(p1);
println(p2);
println(p2_1);
//println(p3);
//println(p4);
}
}
// 运行结果
Address(1 Scala Lane,Anytown,CA,98765)
Address([unknown],Anytown,CA,98765)
Person2(Buck Trend1,None,None)
Person2(Buck Trend2,Some(20),Some(Address(1 Scala Lane,Anytown,CA,98765)))
Person2(Buck Trend2,Some(20),Some(Address(1 Scala Lane,Anytown,CA,98765)))
3. 类的字段
class Name(var value: String)
/*
上述类与下面定义的类相同
class Name(s: String){
private var _value: String = s // 不可见字段
def value: String = _value // getter, 即读方法
def value_ = (newValue: String): Unit = _value = newValue // setter, 即写方法
}
*/
object Test{
def main(args: Array[String]){
val name = new Name("Buck");
println(name.value);
name.value_= ("Bubba"); //与name.value = "Bubba"等价,注意,"_"和"="之间不能留空格
println(name.value);
name.value = "Hank"; // 与name.value_= ("Hank")等价
println(name.value);
}
}
// 运行结果
Buck
Bubba
Hank