Kt學(xué)習(xí)01

kotlin學(xué)習(xí)第一步,初步認(rèn)識并使用

//第一步 hello
fun main(args: Array<String>) {
    println("hello kotlin ")
    PrintHelloWithName("MJ").print();//創(chuàng)建對象不用 new關(guān)鍵字
    PrintHelloWithName(1, 4).print()
}
//構(gòu)造函數(shù)
class ConstructorTest() {
    constructor(name: String) : this() {
    }
    constructor(name: String, age: Int) : this() {
    }
}
class PrintHelloWithName(var name: String) {
    constructor(a: Int, b: Int) : this("") {
        name = "a + b = ${plusNumber(a, b)}"
    }
    constructor(name: String, sex: String) : this(name) {
    }
    //帶入?yún)⒌拇蛴?    fun print() {
        println("hello $name")
    }
    //帶結(jié)構(gòu)體的函數(shù)
    fun plusNumber(a: Int, b: Int): Int {
        return a + b
    }
    //表達(dá)式作為函數(shù)體,返回類型自動推斷:
    fun plusNumber2(a: Int, b: Int) = a + b
    // public 方法則必須明確寫出返回類型
    public fun plusNumber3(a: Int, b: Int): Int = a + b
}

總結(jié)

入?yún)?,返參類型定義都是參數(shù)后面跟;
構(gòu)造函數(shù)比java麻煩一點(diǎn);
創(chuàng)建對象不用new,直接寫函數(shù)名就好;
表達(dá)式作為函數(shù)體,返回類型自動推斷;
public 方法則必須明確寫出返回類型;
Git真jier難下;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容