本「ProgramminginScala-SecondEdition」から抜粋した以下のクラスのaddメソッドを理解しようとしています。これは正しいですか:'add'が取るメソッドは、Rational型の演算子を定義します。新しいRational内で何が起こっているのかわかりません:
numer * that.denom + that.numer * denom,
denom * that.denom
ここでnumerとdenomはどのように割り当てられますか?、各式がコンマで区切られているのはなぜですか?クラス全体:
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 add(that: Rational): Rational =
new Rational(
numer * that.denom + that.numer * denom,
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)
}