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

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.

主站蜘蛛池模板: 新乡市| 灵丘县| 乐安县| 和硕县| 三亚市| 岱山县| 昭苏县| 乐山市| 长泰县| 靖宇县| 云林县| 攀枝花市| 阳东县| 弋阳县| 科技| 丹阳市| 大兴区| 义马市| 且末县| 原平市| 宜州市| 城固县| 巨野县| 尼玛县| 微博| 靖江市| 鹤峰县| 商南县| 密云县| 林甸县| 西吉县| 霍林郭勒市| 阿坝县| 彭水| 怀来县| 沈阳市| 同心县| 邯郸县| 稷山县| 库车县| 永宁县|