1 Star 0 Fork 2

chenjim / kotlin-for-android-developers-zh

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
zen_yao_ding_yi_yi_ge_lei.md 660 Bytes
一键复制 编辑 原始数据 按行查看 历史
WangJie 提交于 2015-10-26 10:38 . Update zen_yao_ding_yi_yi_ge_lei.md

怎么定义一个类

如果你想定义一个类,你只需要使用class关键字。

class MainActivity{

}

它有一个默认唯一的构造器。我们会在以后的课程中学习在特殊的情况下创建其它额外的构造器,但是请记住大部分情况下你只需要这个默认的构造器。你只需要在类名后面写上它的参数。如果这个类没有任何内容可以省略大括号:

class Person(name: String, surname: String)

那么构造函数的函数体在哪呢?你可以写在init块中:

class Person(name: String, surname: String) {
    init{
        ...
    }
}
1
https://gitee.com/chenjim/kotlin-for-android-developers-zh.git
git@gitee.com:chenjim/kotlin-for-android-developers-zh.git
chenjim
kotlin-for-android-developers-zh
kotlin-for-android-developers-zh
master

搜索帮助