* Create control_flow.md * Rename control_flow.md to controlflow.md * Added control flow * Added classes * fix: corrected indentation * fix: corrected indentation
956 B
956 B
title
title |
---|
Classes |
Classes
Basic Usage
Declaration
class
keyword is used to define the class
It is consisted of: class name, class header (type parameters, constructor) and class body (curly braces)
class Person { ... }
Constructor
There are multiple ways to define class constructor.
Primary constructor is part of the class header (name field with data type):
class Person(name: String) { ... }
Secondary constructor: using constructor
keyword inside class body
class Person {
constructor(parent: Person) {
parent.children.add(this)
}
}
Usage
Class instance can be created as regular function
There is no need for new
keyword
val john = Person("John Wayne")