官术网_书友最值得收藏!

Classes

Classes are the foundational type in Kotlin. In Kotlin, a class is a template that provides a state, a behavior, and a type to instances (more on that later).

To define a class, only a name is required:

class VeryBasic

VeryBasic is not very useful, but is still a valid Kotlin syntax.

The VeryBasic class doesn't have any state or behavior; nonetheless, you can declare values of type VeryBasic, as shown in the following code:

fun main(args: Array<String>) {
val basic: VeryBasic = VeryBasic()
}

As you can see, the basic value has a VeryBasic type. To express it differently, basic is an instance of VeryBasic.

In Kotlin, types can be inferred; so, the previous example is equivalent to the following code:

fun main(args: Array<String>) {
val basic = VeryBasic()
}

By being a VeryBasic instance, basic has a copy of the VeryBasic type's state and behavior, namely, none. So sad.

主站蜘蛛池模板: 喀喇| 吉木乃县| 忻州市| 习水县| 呼伦贝尔市| 左云县| 铜陵市| 上虞市| 林西县| 兴化市| 漯河市| 荆门市| 遵义县| 新乡市| 津市市| 上饶市| 巨野县| 张北县| 米易县| 景德镇市| 固始县| 汽车| 江津市| 金沙县| 神农架林区| 南昌县| 朝阳区| 潜山县| 楚雄市| 凤城市| 湖州市| 通山县| 马龙县| 嘉定区| 阳西县| 阿拉善右旗| 阿拉善左旗| 通许县| 杭锦后旗| 郴州市| 临澧县|