July 2018
Intermediate to advanced
400 pages
12h 14m
English
A generic type is a class that accepts an input of any type in its constructor. You are going to begin by defining your own generic type.
Open the Sandbox project and add a new file called Generics.kt. Within Generics.kt, define a LootBox class that specifies a generic type parameter for use with its contents and contains a private property called loot that is assigned the item.
Listing 17.1 Creating a generic type (Generics.kt)
class LootBox<T>(item: T) {
private var loot: T = item
}
You define the LootBox class and make it generic by specifying a generic type parameter for use with the class, written as T and specified within diamond braces (< >) like other type parameters. The generic type parameter, ...
Read now
Unlock full access