- Kotlin Programming By Example
- Iyanu Adelekan
- 128字
- 2021-08-27 20:00:12
Working with classes
A class is declared using the class keyword followed by the class name:
class Person
As in the preceding example, a class in Kotlin need not have a body. Though this is charming, almost all the time you will want your class to have characteristics and behaviors placed within a body. This can be done with the use of opening and closing brackets:
class HelloPrinter {
fun printHello() {
println("Hello!")
}
}
In the preceding code snippet, we have a class named HelloPrinter with a single function declared in it. A function that is declared within a class is called a method. Methods can also be referred to as behaviors. Once a method is declared, it can be used by all instances of the class.
推薦閱讀
- 程序員面試白皮書
- Instant Apache Stanbol
- 算法訓練營:入門篇(全彩版)
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- Android 7編程入門經典:使用Android Studio 2(第4版)
- Mastering C# Concurrency
- FLL+WRO樂高機器人競賽教程:機械、巡線與PID
- Haskell Data Analysis Cookbook
- Python機器學習算法: 原理、實現與案例
- Learning Apache Karaf
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎卷)
- Django 3.0入門與實踐
- 快速入門與進階:Creo 4·0全實例精講
- 深入解析Java編譯器:源碼剖析與實例詳解
- Extending Docker