函数式对象
本章的重点在于定义函数式对象,也就是说,没有任何可变状态的对象的类。作为运行的例子,我们将创造若干把分数作为不可变对象建模的类的变体。在这过程中,我们会展示给你 Scala 面向对象编程的更多方面:类参数和构造函数,方法和操作符,私有成员,子类方法重载,先决条件检查,同类方法重载和自指向。
1.类Rational的式样书
分数:rational number 是一种可以表达为比率 dn 的数字,这里的 n 和 d 是数字,其中d 不能为零。n被称作是分子:numerator,d 被称作是分母:denominator。本章我们将要设计的类必须模型化分数的行为,包括允许它们执行加,减,乘还有除运算。
本章还将捎带提一些 Scala 让你写出感觉像原生语言支持的库的方法。例如,在本章结尾你将能用 Rational 类这样做:
scala> val oneHalf = new Rational(1, 2)
oneHalf: Rational = 1/2
scala> val twoThirds = new Rational(2, 3)
twoThirds: Rational = 2/3
scala> (oneHalf / 7) + (1 twoThirds)
res0: Rational = 17/42
2.创建Rational
class Rational(n:Int, d:Int)
Scala 编译器将把你放在类内部的任何不是字段的部分或者方法定义的代码,编译进主构造器。例如,你可以像这样打印输出一条除错消息:
class Rational(n: Int, d: Int) {
println("Created "+n+"/"+d)
}
<div class="page" title="Page 85"><div class="layoutArea"><div class="column"><p><span style="font-size: 9.000000pt; font-family: 'LuxiMono'">scala> </span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'; color: rgb(0.000000%, 0.000000%, 90.200000%)">new </span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'; color: rgb(40.000000%, 0.000000%, 60.400000%)">Rational</span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'">(</span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'; color: rgb(80.400000%, 48.200000%, 0.000000%)">1</span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'">, </span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'; color: rgb(80.400000%, 48.200000%, 0.000000%)">2</span><span style="font-size: 9.000000pt; font-family: 'LuxiMono'">) </span></p></div></div></div><div class="page" title="Page 86"><div class="layoutArea"><div class="column"><pre><span style="font-size: 9.000000pt; font-family: 'LuxiMono'; color: rgb(35.300000%, 0.000000%, 0.000000%)">Created 1/2
res0: Rational = Rational@a0b0f5</span>
3.带操作符方法的 Rational
class Rational(n: Int, d: Int) {
require(d != 0)
private val g = gcd(n.abs, d.abs)
val numer = n / g
val denom = d / g
def this(n: Int) = this(n, 1)
def +(that: Rational): Rational =
new Rational(
numer * that.denom + that.numer * denom,
denom * that.denom
)
def *(that: Rational): Rational =
new Rational(numer * that.numer, denom * that.denom)
override def toString = numer+"/"+denom
private def gcd(a: Int, b: Int): Int =
if (b == 0) a else gcd(b, a % b)
}