大數(shù)據(jù)教程為大家分享Scala系列之方法的嵌套和方法多態(tài)
方法里嵌套定義其他方法
示例1
object EmbedDemo {
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計制作、成都網(wǎng)站設(shè)計與策劃設(shè)計,瀘溪網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十余年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:瀘溪等地區(qū)。瀘溪做網(wǎng)站價格咨詢:18982081108
def add3(x:Int,y:Int,z:Int)={
def add2(x:Int,y:Int)={
x+y
}
add2(add2(x,y),z)
}
def main(args: Array[String]): Unit = {
println(add3(1,2,3)) //6
}
}
示例2
def factorial(x: Int): Int = {
def fact(x: Int, accumulator: Int): Int = {
if (x <= 1) accumulator
else fact(x - 1, x * accumulator)
}
fact(x, 1)
}
println("Factorial of 2: " + factorial(2))
println("Factorial of 3: " + factorial(3))
方法的多態(tài)
Scala里方法可以通過類型實現(xiàn)參數(shù)化,類似泛型。
def listOfDuplicates[A](x: A, length: Int): List[A] = {
if (length < 1)
Nil
else
x :: listOfDuplicates(x, length - 1)
}
println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3)
println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La)
文章名稱:大數(shù)據(jù)教程Scala系列之方法的嵌套和方法多態(tài)
分享地址:http://aaarwkj.com/article30/gjogpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、、網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)