快学Scala 第6章 对象 - 练习】的更多相关文章

1. 编写一个Conversions对象,加入inchesToCentimeters.gallonsToLiters和milesToKilometers方法. object Conversions {     def main(args: Array[String]){         printf("1 inch = %g centimeters\n", inchesToCentimeters(1))         printf("2 gallons = %g liter…
1 简介 近期对Scala比较感兴趣,买了本<快学Scala>,感觉不错.比<Programming Scala:Tackle Multi-Core Complexity on the Java Virtual Machine>好很多. 是本不错的入门书.而且每个章节都设置了难度级别,每章有习题,可以巩固Scala语法. 本文的目的就是针对这些习题进行解答 2 基础  2.1 在Scala REPL中键入3,然后按Tab键.有哪些方法可以被应用? 这个....直接操作一遍就有结果了…
Robert Peng's Blog - https://mr-dai.github.io/ <快学Scala>Intro与第1章 - https://mr-dai.github.io/Scala-for-the-Impatient-1 <快学Scala>第2章(上):控制结构 - https://mr-dai.github.io/Scala-for-the-Impatient-2-1 <快学Scala>第2章(下):函数 - https://mr-dai.github…
trait的abstract override使用: 当我看到abstract override介绍的时候也是一脸懵逼,因为快学scala,只介绍了因为TimestampLogger中调用的super.log依旧是个abstract class,所以必须在方法前加上abstract和override.但是并没有具体介绍如何使用,然后查阅了其他文档,才明白使用方法. 下面的代码定义了超类LoggerEmpty,这个定义意味着该特质只能混入扩展LoggerEmpty的类中. 在特质中声明抽象方法中有…
Scala 类型层级: 对象相等性: 和Java一样要重写equals方法和hashcode方法 class Student(val id: Int, val name: String) { override def equals(other: Any) = { val that = other.asInstanceOf[Student] if (that == null) false else id == that.id && name == that.name } override d…
4 映射和元组  4.1 设置一个映射,当中包括你想要的一些装备,以及它们的价格.然后构建还有一个映射.採用同一组键,可是价格上打9折 映射的简单操作  ,"gun"->18,"ipad"->1000)   , gun -> 18, ipad -> 1000)      scala> for((k,v) <- map) yield (k,v * 0.9)   res3: scala.collection.immutable.Map…
5 类  5.1 改进5.1节的Counter类,让它不要在Int.MaxValue时变成负数 class Count{ private var value = Int.MaxValue else value } def current = value } 5.2 编写一个BankAccount类,增加deposit和withdraw方法,和一个仅仅读的balance属性 ){ def deposit(){} def withdraw(){} } 5.3 编写一个Time类,增加仅仅读属性hou…