October 2021
Intermediate to advanced
500 pages
16h 23m
English
A generic type is a class that accepts an input of any type in its constructor (though there can be limits on the type, as you will see later in this chapter). You will begin by defining your own generic type.
In NyetHack, create a new Kotlin file called Loot.kt. Within your new file, define a LootBox class that specifies a generic type parameter for use with its contents and contains a private property called contents that is assigned the item.
Listing 18.1 Creating a generic class (Loot.kt)
class LootBox<T>(var contents: T)
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 ...
Read now
Unlock full access