scala - 类:构造器

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值