为了更好的驾驭spark,最近在学习scala语言特性,主要看《快学scala》,顺便把一些自己认为有用的代码记下来。
package examples class Fraction(n:Int,d:Int) { private val num =n private val den=d def *(other:Fraction) = new Fraction(num*other.num,den*other.den) //重载称号操作符 } object Fraction { def apply(n:Int,d:Int) = new Fraction(n,d) def unapply(input:Fraction)= { if(input.den==0) None else Some((input.num,input.den)) } } object Example11 extends App{ //元组提取器 val Fraction(a,b) = Fraction(1,2)*Fraction(1,3) println(a,b) //额外, 单个参数或者无参数的需要用option;提取任意长度的需要用unapplaySeq }
本文作者:linger
本文链接:http://blog.csdn.net/lingerlanlan/article/details/44837731
时间: 2024-08-02 11:02:31