成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Scala具名參數(shù)NamedArgument-創(chuàng)新互聯(lián)

什么是具名參數(shù)呢?

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供寶安網(wǎng)站建設、寶安做網(wǎng)站、寶安網(wǎng)站設計、寶安網(wǎng)站制作等企業(yè)網(wǎng)站建設、網(wǎng)頁設計與制作、寶安企業(yè)網(wǎng)站模板建站服務,十余年寶安做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡服務。

下面通過簡單例子來說明。

需求:有一個42歲的父親和一個12歲的孩子,求父親比孩子年長多少歲?可以很簡單的寫出下面的函數(shù)體來實現(xiàn),如下圖:

Scala具名參數(shù)Named Argument

特殊情況來了,如果在使用getDvalue函數(shù)時,不小心導致了參數(shù)p和c寫顛倒了,那么結(jié)果會是下圖所示:

Scala具名參數(shù)Named Argument

這樣就不是所要的結(jié)果了,很小的疏忽導致了結(jié)果的不正確性,做一下簡單的修改,如下圖所示:

Scala具名參數(shù)Named Argument

結(jié)果又是我們最開始所需的了。

下面寫幾個錯誤的示例:

Scala具名參數(shù)Named Argument

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ù)時應按照函數(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ù)

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

網(wǎng)站題目:Scala具名參數(shù)NamedArgument-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://jinyejixie.com/article40/dshgho.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、動態(tài)網(wǎng)站、品牌網(wǎng)站建設、外貿(mào)建站網(wǎng)站收錄網(wǎng)站建設

廣告

聲明:本網(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)

外貿(mào)網(wǎng)站建設
祁连县| 林口县| 南木林县| 香河县| 额尔古纳市| 志丹县| 新竹县| 淳化县| 泰安市| 保德县| 石柱| 思茅市| 夏邑县| 张家口市| 无锡市| 迭部县| 尉氏县| 丽江市| 沽源县| 安阳县| 岐山县| 黄陵县| 南涧| 河津市| 冕宁县| 甘洛县| 聂拉木县| 车险| 陇南市| 府谷县| 荣昌县| 新郑市| 常山县| 凉城县| 正定县| 临江市| 环江| 巴青县| 宁阳县| 福建省| 汉沽区|