Kotlin Tour

joow
942 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Classes

Introduction

https://kotlinlang.org/docs/reference/classes.html#classes

The keyword class is used to declare a class :

class Empty

Constructors

https://kotlinlang.org/docs/reference/classes.html#constructors

Classes can have one primary constructor and multiple secondary constructors.

Create a class Person with two properties : firstName and lastName
1
class Person
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create a class Rectange and allow to create easily a square
1
class Rectangle
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Property

https://kotlinlang.org/docs/reference/properties.html

There is no such thing as a field in a Kotlin class. Instead classes may have properties.

Add a computed property area to the Rectangle class
1
class Rectangle
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create a RandomInt class that can return a new random Int and count the number of Ints generated
1
class RandomInt
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Add a factorial property to BigInteger type
1
2
3
4
import java.math.BigInteger
val BigInteger.factorial: BigInteger
get() = BigInteger.ZERO
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Data classes

https://kotlinlang.org/docs/reference/data-classes.html

Using the magic data keyword we can transform a class in a data class having the following code handled for us :

- `equals()`/`hashCode()`
- `toString()`
- `componentN()` functions for each declared property
- `copy()` function
Transform the Person class in a data class and use it to copy a person, changing only his first name
1
class Person
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Pair and Triple

https://kotlinlang.org/docs/reference/data-classes.html#standard-data-classes

Pair and Triple classes are data classes allowing to return a pair or a triple of elements.

Add an identity function to the Person class return the first name and the last name as a pair
1
class Person
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Destructuring

https://kotlinlang.org/docs/reference/multi-declarations.html

Data classes can be destructured :

val (country, city) = Pair("France", "Bordeaux")
val (_, city) = Pair("France", "Bordeaux")       // don't care about country
Add a hello function taking a Person and returning Hello, <firstName> <lastName>, using destructuring
1
fun hello() = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Equality

https://kotlinlang.org/docs/reference/equality.html

There are two equality operators in Kotlin :

  1. == (and !=) as structural equality (or functional equality)
  2. === (and !==) as referential equality
Define the structural equality for our Rectangle class
1
class Rectangle
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content