Scala类&对象(一)

2018-03-17  本文已影响0人  allenggg

学习笔记摘抄于Twitter scala文档:Twitter.github.io
为了方便,本文中类的定义代码和调用代码同放一处;
实际上,代码的调用需要写在程序入口(main方法)
next:Scala类&对象(二)

一、类的定义与创建

一个简单的定义 & 调用

class Calculator {
  val brand: String = "HP"
  def add(m: Int, n: Int): Int = m + n
 }
val calc = new Calculator //创建对象
val res = calc.add(1, 2) //调用方法
val brand = calc.brand  //获取成员

上面的例子展示了如何在类中用def定义方法和用val定义字段值。方法就是可以访问类的状态的函数。

构造函数

构造函数不是特殊的方法,他们是除了类的方法定义之外的代码
让我们扩展计算器的例子,增加一个构造函数参数,并用它来初始化内部状态。

class Calculator(brand: String) {
  /**
   * A constructor.
   */
  val color: String = if (brand == "TI") {
    "blue"
  } else if (brand == "HP") {
    "black"
  } else {
    "white"
  }

  // An instance method.
  def add(m: Int, n: Int): Int = m + n
}

val calc = new Calculator("HP")
calc.color //black

函数 vs 方法

函数和方法在很大程度上是可以互换的
由于函数和方法是如此的相似,你可能都不知道你调用的东西是一个函数还是一个方法

class C {
  var acc = 0
  def minc = { acc += 1 }  //方法
  val finc = { () => acc += 1 } //函数
}

val c = new C
c.minc // call method
c.finc // returns the function as a value

二、继承

class ScientificCalculator(brand: String) extends Calculator(brand) {
  def log(m: Double, base: Double) = math.log(m) / math.log(base)
}

重载方法

class EvenMoreScientificCalculator(brand: String) extends ScientificCalculator(brand) {
  def log(m: Int): Double = log(m, math.exp(1))
}

三、抽象类

abstract class Shape {
  def getArea: Int  // subclass should define this
}
class Circle(r: Int) extends Shape {
  def getArea: Int = r * r * 3 //pie取3
}
val c = new Circle(2) //不能创建抽象类的instance,可创建子类instance
val area = c.getArea //area: 12

四、特质(trait)

特质是一些字段和行为的集合,可以扩展或混入(mixin)你的类中。

trait Car {
  val brand: String
}

trait Shiny {
  val shineRefraction: Int
}
class BMW extends Car {
  val brand = "BMW"
}

通过with关键字,一个类可以扩展多个特质:

class BMW extends Car with Shiny {
  val brand = "BMW"
  val shineRefraction = 12
}

什么时候应该使用特质而不是抽象类?

五、类型

函数也可以是泛型的,来适用于所有类型。当这种情况发生时,你会看到用方括号语法引入的类型参数。下面的例子展示了一个使用泛型键和值的缓存。

trait Cache[K, V] {
  def get(key: K): V
  def put(key: K, value: V)
  def delete(key: K)
}

除了类外,方法也可使用泛型

def remove[K](key: K) //方法也可以引入类型参数

next:Scala类&对象(二)

上一篇 下一篇

猜你喜欢

热点阅读