首页  

scala case class     所属分类 scala 浏览量 612
case类 自动生成 toString hashCode equals
构造器参数 默认为val类型
自动创建伴生对象,在伴生对象中实现apply方法


class ManNotCase(name: String) {}

case class Man(name: String) {}

object CaseClassDemo {
  def main(args: Array[String]) {
    val manNotCase1 = new ManNotCase("cat");
    val manNotCase2 = new ManNotCase("cat");
    // false
    println(manNotCase1 == manNotCase2);
    // 1057941451,demo.ManNotCase@3f0ee7cb
    println(manNotCase1.hashCode() + "," + manNotCase1.toString);
    println(manNotCase2.hashCode() + "," + manNotCase2.toString);

    val man1 = Man("cat");
    val man2 = Man("cat");
    // true
    println(man1 == man2);
    // 1862043439,Man(cat)
    println(man1.hashCode() + "," + man1.toString);
    println(man2.hashCode() + "," + man2.toString);

  }
}

上一篇     下一篇
scala Any AnyRef Object AnyVal

Scala 异常

scala 函数

How is Akka used in Play

akka actor demo

akka http rest api demo