Lesson 31: OOP – Enum class in kotlin Language

Lesson 31: OOP – Enum class in kotlin Language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

In Kotlin we have another special type of class that is declared with the keywords enum class.

They are used to define a set of constants.

Problem 1

Declare an enum class with the names of cards in the English deck.
Define a class class that has a property of the class enum class.

Project131 – Home.kt

enum class TypeCarta {
    DIAMOND,
    CLOVER,
    HEART,
    PICA
}

class letter (val type: TypeCarta, val value: Int) {

    fun print () {
        println ("Chart: $type and its value is $value")
    }
}

fun main (parameter: Array <String>) {
    val letter1 = letter (TypeCarta.CLOVER, 4)
    letter1.print ()
}

To declare an enum class we indicate each of the possible constants that can be saved:

Enum class TypeCarta {
    DIAMOND,
    CLOVER,
    HEART,
    PICA
}

The Charter class has two properties, the first type call that is of the enum class TypeCarta and the second the value of the letter:

Class Letter (val type: TypeCarta, val value: Int) {

The print method shows the contents of the type property and the value property:

    Fun print () {
        Println ("Chart: $type and its value is $value")
    }

In the main function, we create an object of the class Charter and we pass in the first parameter some of the four constants defined within the enum class TipoCarta:

Fun main (parameter: Array <String>) {
    Val Letter1 = Carta (TypeCarta.CLOVER, 4)
    Letter1.print ()
}

Properties in an enum class in kotlin

Like common classes, the enum class can have properties defined in the constructor. Then we must indicate a value for each of these properties in the defined constants.

Problem 2

Declare an enum class that represents the four basic operations, associate to each constant a String with the sign of the operation.
Define an operation class that defines three properties, the first two must be the numbers and the third the type of operation.

Project132 – Principal.kt

enum class TypeOperation (val type: String) {
    SUM ("+"),
    SUBTRACTION("-"),
    MULTIPLICATION("*"),
    DIVISION("/")
}

class Operation (val value1: Int, val value2: Int, val typeOperation: TypeOperation) {

    fun operate () {
        var result: Int = 0
        when (typeOperation) {

            TypeOperation.SUM -> result = value1 + value2
            TypeOperation.SUBTRACTION -> result = value1 - value2
            TypeOperation.MULTIPLICATION -> result = value1 * value2
            TypeOperation.DIVISION -> result = value1 / value2
            
        }
        println ("$value1 ${typeOperation.type} $value2 is equal to $result")
    }
}

fun main (parameter: Array <String>) {
    val operation1 = Operation (10, 4, TypeOperation.SUM)
    operation1.operate ()
}

We have declared the TypeOperation class with four constant calls: SUM, SUBTRACTION, MULTIPLICATION, and DIVISION.
Each constant has in parentheses the value of the type property that is a String:

Enum class TypeOperation (val type: String) {
    SUM ("+"),
    SUBTRACTION("-"),
    MULTIPLICATION("*"),
    DIVISION("/")
}

We declare the class Operation that receives two integers and an object of type TypeOperation:

Class Operation (val value1: Int, val value2: Int, val typeOperation: TypeOperation) {

The method to operate according to the value stored in the typeOperation property stores the respective value in the result variable:

    Fun operate () {
        Var result: Int = 0
        When (typeOperation) {
            TypeOperation.SUM -> result = value1 + value2
            TypeOperacion.SUBTRACTION -> result = value1 - value2
            TypeOperation.MULTIPLICATION -> result = value1 * value2
            TypeOperation.DIVISION -> result = value1 / value2
        }

To print the used operator (“+”, “-“, etc.), we can access the type property of the type objectOperation:

        Println ("$value1 ${typeOperation.type} $value2 is equal to $result")

In the main function we define an object of the class Operation and we pass the two integers to operate and the type of operation to be carried out:

Fun main (parameter: Array <String>) {
    Val operation1 = Operation (10, 4, TypeOperation.SUM)
    Operation1.operate()
}

Acotaciones

Other important data that we can extract from an enum class is the position of a constant in the enumeration by ordinal property:

Enum class TypeOperation (val type: String) {
    SUM ("+"),
    SUBTRACTION("-"),
    MULTIPLICATION("*"),
    DIVISION("/")
}

Fun main (parameter: Array <String>) {
    Val type1 = TypeOperacion.MULTIPLICACION
    Println (type1) // MULTIPLICATION
    Println (type1.type) // *
    Println (type1.ordinal) // 2
}

With the ordinal property, we get the position of the constant inside the enum class (the first constant occupies position 0, the second position 2 and so on.

If we need to go through a for all constants contained in an enum class the syntax is as follows:

Enum class TypeOperation (val type: String) {
    SUM ("+"),
    SUBTRACTION("-"),
    MULTIPLICATION("*"),
    DIVISION("/")
}

Fun main (parameter: Array <String>) {
    For (element in TypeOperation.values ​​()) {
        Println (element)
    }
}

By valueOf we can pass a String with a constant and return a reference to that constant:

Enum class TypeOperation (val type: String) {
    SUM ("+"),
    SUBTRACTION("-"),
    MULTIPLICATION("*"),
    DIVISION("/")
}

Fun main (parameter: Array <String>) {
    Val type1 = TypeOperation.valueOf ("RESET")
    Println (type1) // RESET
    Println (type1.type) // -
    Println (type1.ordinal) // 1
}

As each constant is an object we can override the toString method and make it return a string other than the constant name:

Enum class TypeOperation (val type: String) {
    SUM ("+") {
        Override fun toString () = "sum operation"
    },
    SUBTRACTION("-"){
        Override fun toString () = "operation subtracts"
    },
    MULTIPLICATION("*"),
    DIVISION("/")
}

Fun main (parameter: Array <String>) {
    Val tipo1 = TipoOperacion.SUMA
    Println (type1) // sum operation
    Println (type1.type) // +
    Println (type1.ordinal) // 0
}

Proposed problem

  • Define an enum class storing as constant the names of several South American countries and as property for each country, the amount of inhabitants that it has.
    Define a variable of this type and print the constant and the number of inhabitants of that variable.
Solution

About The Author

Related posts

Leave a Reply