1,定義函數(shù)
站在用戶的角度思考問題,與客戶深入溝通,找到湖口網(wǎng)站設(shè)計與湖口網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬主機、企業(yè)郵箱。業(yè)務(wù)覆蓋湖口地區(qū)。fun?函數(shù)名(參數(shù)名:類型,參數(shù)名:類型,...):返回類型{ ????...... }比如
fun?sum(a:?Int,?b:?Int):?Int?{ ????return?a?+?b }如果不需要返回值,則可以
fun?printSum(a:?Int,?b:?Int)?{ ????println("sum?of?$a?and?$b?is?${a?+?b}") }2,定義變量
如果是只讀變量,用val聲明,如果是可修改的變量,用var聲明
val?a:?Int?=?1?? val?b?=?2???//?自動推斷類型`Int`? val?c:?Int??//?如果沒有初始值,則需要提供類型 c?=?3???????//?稍后賦值 var?x?=?5?//?自動推斷類型`Int` x?+=?13,字符串模板
var?a?=?1 //?simple?name?in?template: val?s1?=?"a?is?$a"? a?=?2 //?arbitrary?expression?in?template: val?s2?=?"${s1.replace("is",?"was")},?but?now?is?$a"4,if表達(dá)式
fun?maxOf(a:?Int,?b:?Int)?=?if?(a?>?b)?a?else?b5,對于可能為null的值,必須判斷
fun?parseInt(str:?String):?Int??{ ????//?如果不是int,就返回null } fun?printProduct(arg1:?String,?arg2:?String)?{ ????val?x?=?parseInt(arg1) ????val?y?=?parseInt(arg2) ????//?Using?`x?*?y`?yields?error?because?they?may?hold?nulls. ????if?(x?!=?null?&&?y?!=?null)?{ ????????//?x?and?y?are?automatically?cast?to?non-nullable?after?null?check ????????println(x?*?y) ????} ????else?{ ????????println("either?'$arg1'?or?'$arg2'?is?not?a?number") ????}???? }6, 用is 關(guān)鍵字判斷對象類型,相當(dāng)于java的instanceOf
fun?getStringLength(obj:?Any):?Int??{ ????if?(obj?!is?String)?return?null ????//?`obj`?is?automatically?cast?to?`String`?in?this?branch ????return?obj.length }7, list遍歷
val?items?=?listOf("apple",?"banana",?"kiwifruit") for?(item?in?items)?{ ????println(item) } val?items?=?listOf("apple",?"banana",?"kiwifruit") var?index?=?0 while?(index?fun?describe(obj:?Any):?String?= ????when?(obj)?{ ????????1??????????->?"One" ????????"Hello"????->?"Greeting" ????????is?Long????->?"Long" ????????!is?String?->?"Not?a?string" ????????else???????->?"Unknown" ????}8, 范圍
val?x?=?10 val?y?=?9 if?(x?in?1..y+1)?{ ????println("fits?in?range") } val?list?=?listOf("a",?"b",?"c") if?(-1?!in?0..list.lastIndex)?{ ????println("-1?is?out?of?range") } if?(list.size?!in?list.indices)?{ ????println("list?size?is?out?of?valid?list?indices?range,?too") } //遍歷 for?(x?in?1..5)?{ ????print(x) } //步長 for?(x?in?1..10?step?2)?{ ????print(x) } println() for?(x?in?9?downTo?0?step?3)?{ ????print(x) }9,集合
for?(item?in?items)?{ ????println(item) } when?{ ????"orange"?in?items?->?println("juicy") ????"apple"?in?items?->?println("apple?is?fine?too") } //lambda表達(dá)式 val?fruits?=?listOf("banana",?"avocado",?"apple",?"kiwifruit") fruits ??.filter?{?it.startsWith("a")?} ??.sortedBy?{?it?} ??.map?{?it.toUpperCase()?} ??.forEach?{?println(it)?}10,創(chuàng)建對象
val?rectangle?=?Rectangle(5.0,?2.0)?//不需要'new' val?triangle?=?Triangle(3.0,?4.0,?5.0)參考文獻(xiàn):?https://kotlinlang.org/docs/reference/coding-conventions.html
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。