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難下;