Kt学习01

kotlin学习第一步,初步认识并使用

//第一步 hello
fun main(args: Array<String>) {
    println("hello kotlin ")
    PrintHelloWithName("MJ").print();//创建对象不用 new关键字
    PrintHelloWithName(1, 4).print()
}
//构造函数
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) {
    }
    //带入参的打印
    fun print() {
        println("hello $name")
    }
    //带结构体的函数
    fun plusNumber(a: Int, b: Int): Int {
        return a + b
    }
    //表达式作为函数体,返回类型自动推断:
    fun plusNumber2(a: Int, b: Int) = a + b
    // public 方法则必须明确写出返回类型
    public fun plusNumber3(a: Int, b: Int): Int = a + b
}

总结

入参,返参类型定义都是参数后面跟
构造函数比java麻烦一点;
创建对象不用new,直接写函数名就好;
表达式作为函数体,返回类型自动推断;
public 方法则必须明确写出返回类型;
Git真jier难下;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容