October 2018
Intermediate to advanced
370 pages
9h 15m
English
Just as the enum class can have a property, it can also have a function. Let's create an application in which we can store the land area of different states:
enum class US (val totalArea : Double, val landArea : Double) { NEWYORK (141_297.0, 122_057.0), VIRGINIA (110_787.0, 102_279.0) , HAWAII (28_313.0, 16_635.0), NEWJERSEY (22_591.0, 19_047.0) ; fun getWaterArea() = totalArea - landArea }
Although we don't often use semicolons in Kotlin, we do need to place a semicolon at the end of a list when the enum class contains a constructor with more than one property. enum class US contains different states, each of which contain information about the land and total area. The enum class contains the getWaterArea ...
Read now
Unlock full access