什么是具名參數(shù)呢?
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供寶安網(wǎng)站建設(shè)、寶安做網(wǎng)站、寶安網(wǎng)站設(shè)計、寶安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、寶安企業(yè)網(wǎng)站模板建站服務(wù),十余年寶安做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。下面通過簡單例子來說明。
需求:有一個42歲的父親和一個12歲的孩子,求父親比孩子年長多少歲?可以很簡單的寫出下面的函數(shù)體來實現(xiàn),如下圖:
特殊情況來了,如果在使用getDvalue函數(shù)時,不小心導(dǎo)致了參數(shù)p和c寫顛倒了,那么結(jié)果會是下圖所示:
這樣就不是所要的結(jié)果了,很小的疏忽導(dǎo)致了結(jié)果的不正確性,做一下簡單的修改,如下圖所示:
結(jié)果又是我們最開始所需的了。
下面寫幾個錯誤的示例:
code:
package demo
object NamedArgument {
def main(args: Array[String]): Unit = {
val parent=42
val child=12
def getDvalue(p:Int,c:Int):Int={
p-c
}
println("d-value is :"+getDvalue(c=child,p=parent))
def testNA(a:Int,b:String)={}
testNA(1,"string")//right
testNA("string",1)//Multiple markers at this line:
//type mismatch; found : String("string") required: Int
//type mismatch; found : Int(1) required: String
//如果不使用具名參數(shù),調(diào)用函數(shù)時應(yīng)按照函數(shù)定義的參數(shù)順序填寫參數(shù)
testNA(b="string",a=1)//right 具名參數(shù)
testNA(b="string",1)//positional after named argument.
//如果函數(shù)中某個參數(shù)沒有具名,則按照其所在位置指定參數(shù),可以叫做位置參數(shù)
testNA(b=1,a="string") //Multiple markers at this line:
//type mismatch; found : String("string") required: Int
//type mismatch; found : Int(1) required: String
//type mismatch; found : String("string") required: Int
//具名參數(shù)指定類型錯誤,這是比較明顯的錯誤
testNA(1,a="string") //parameter 'a' is already specified at parameter position 1
//參數(shù)a已經(jīng)在位置1(a="string")定義過,與位置參數(shù)1(所在位置0)沖突
testNA(1,b="string") //right
testNA(a=1,"string") //right
}
}
接下來試試其它的類型
def testNA2(a:Int*,b:String)={} //*-parameter must come last 這是基本語法的約束,可變參數(shù)必須放在最后,這種寫法是錯誤的
def testNA2(a:String,b:Int*)={} //right
testNA2("string",1,2,3,4)//right
testNA2(a="string",b=1,2,3,4)//right
testNA2(1,2,3,4,"string")//type mismatch; found : Int(1) required: String
//type mismatch; found : String("string") required: Int
testNA2(b=1,2,3,4,a="string")
testNA2(b= List(1,2,3,4),a="string")//type mismatch; found : List[Int] required: Int
//這樣會提示類型錯誤,這樣寫其實就不是可變參數(shù)了
//可以寫成如下形式testNA3
def testNA3(a:String,b:List[Int])={}
testNA3(b= List(1,2,3,4),a="string")
//匿名參數(shù)
def testNA4(a:Int,b:String)=a
val t1: Int=>Int = testNA4(_:Int,"string")
val t2: Int => Int = testNA4(b = "string", a = _) //positional after named argument.
val t3 = testNA4(b = "string", a = _: Int) //positional after named argument.
val t4: Int=>Int = testNA4(_ , b="string") //right
val t5: Int=>Int = testNA4(_ , "string") //right
val t6: Int=>Int = testNA4(a=_ , b="string") //type mismatch; found : Int required: Int=>Int
//匿名參數(shù)不能使用具名參數(shù)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:Scala具名參數(shù)NamedArgument-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://aaarwkj.com/article40/dshgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、動態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站收錄、網(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)