var a =
"13"
var b =
13a = b.
tostring()
b = a.
toint
()
fun
main
(args:array
)
fun
main
(args:array
)catch
(e:exception)
println
("$(num1)+$(num2)=$(num1+num2)"
)}
fun
main
(args:array)"
)}funfact
(num:int)
:int
else
}fun
bigfact
(num:biginteger)
:biginteger
else
}
fun
main
(args:array
)tailrec
funolladd
(num:int, result:int)
:int
else
}
class
rect
(var height:int,
var width:int)
class
girl
(var charctor:string,
var voice:string)
funmain
(args:array)"
)println
("寬度:$")
var girl1 =
girl
("彪悍"
,"甜美"
)println
("girl1這位妹子的聲音:$")
}/*輸出:高度:20
寬度:10
girl1這位妹子的聲音:甜美
*/
class
girl
(var charctor:string,
var voice:string)
funcry()
}fun
main
(args:array)"
) girl1.
cry(
) girl1.
smaile()
}
/**
* 生產方建立的洗衣機
**/class
washmachine
(var module:string,
var size:int)
funclosedoor()
funselectmode()
}fun
start()
else
1->
2->
else
->}}
}}/*** 使用者使用洗衣機
**/fun
main
(args:array
)
/**
* 生產方建立的洗衣機
**/class
washmachine
(var module:string,
var size:int)
funclosedoor()
funselectmode()
}private
funsetmotorspeed
(speed:int)
轉/秒")}
funstart()
else
1->
2->
else
->}}
}}/*** 使用者使用洗衣機
**/fun
main
(args:array
)
/*
父類*/
open
class fathor
funmoney()
}/*子類*/
class son:
fathor()
}fun
main
(args:array)"
) son1.
action()
son1.
money()
}/*輸出:兒子的性格內向
兒子很乖,在公共場合很有禮貌
很有錢*/
abstract
class
human
(var name:string)
class
man(name:string)
:human
(name)
哇哇哇地大口吃")}
}class
woman
(name:string)
:human
(name)
嗚嗚嗚地小口吃")}
}fun
main
(args:array
)
abstract
class
human
(var name:string)
class
man(name:string)
:human
(name)
哇哇哇地大口吃")}
override
funpee()
站著撒尿")}
}class
woman
(name:string)
:human
(name)
嗚嗚嗚地小口吃")}
override
funpee()
蹲著撒尿")}
}fun
main
(args:array
)for
(person in houselist)
}
介面是事物的能力,而抽象類是事物的本質
inte***ce iman
abstract
class human
class man:
human()
,iman
override
funeat()
}class taijian:
human()
}fun
main
(args:array)}
}
inte***ce iwashbow1
class bigheadson:iwashbowl
}class smallheadfather:iwashbowl by
bigheadson()
}fun
main
(args:array
)
object bigheadson:iwashbowl
}class smallheadfather:iwashbowl by bigheadson
}
enum
class week
funmain
(args:array
)
sealed
class son
class 小小驢():
son(
)class 小騾子():
son()}
funmain
(args:array)}
}
Kotlin 基礎語法
1.包宣告 1 檔案開頭宣告,預設 default 包。如 package com.strugglelin.kotlin 2 預設匯入的包 kotlin.kotlin.annotation.kotlin.collections.kotlin.comparisons.kotlin.io.kotlin....
kotlin基本語法
定義函式開始 帶有兩個int引數 返回int的函式 fun sum a int,b int int 將表示式作為函式體 返回值型別自動推斷的函式 fun sum2 a int,b int a b 函式返回無意義的值 fun printsum a int,b int unit unit返回型別可以省略...
Kotlin基本語法
包的定義與導人 package my.demo import kotlin.text.程式入口點 fun main 函式 帶有兩個 int 引數 返回 int 的函式 fun sum a int,b int int fun main 將表示式作為函式體 返回值型別自動推斷的函式 fun sum a ...