溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Kotlin入門基礎(chǔ)1--一句話教程

發(fā)布時(shí)間:2020-03-10 09:40:01 來(lái)源:網(wǎng)絡(luò) 閱讀:432 作者:北京看看 欄目:開發(fā)技術(shù)

1,定義函數(shù)

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???//?自動(dòng)推斷類型`Int`?
val?c:?Int??//?如果沒有初始值,則需要提供類型
c?=?3???????//?稍后賦值
var?x?=?5?//?自動(dòng)推斷類型`Int`
x?+=?1

3,字符串模板

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?b

5,對(duì)于可能為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)鍵字判斷對(duì)象類型,相當(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?<?items.size)?{
????println("item?at?$index?is?${items[index]}")
????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)
}
//步長(zhǎng)
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)建對(duì)象

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI