Touching kotlin for the first time-Enum Classes

Overview

Contents

Basic definition-same as java

enum class Weather {
  SUNNY, CLOUDY, RAINY
}

Initialization-unlike java

enum class Weather(val weather: String) {
  SUNNY("sunny"),
  CLOUDY("cloudy"),
  RAINY("rainy")
}

name and ordinal --same as java

println(Weather.CLOUDY.name) // CLOUNDY
println(Weather.CLOUDY.ordinal) // 1

valueOf () and values () --same as java

println(Weather.valueOf("sunny".toUpperCase())) // SUNNY
for (weather: Weather in Weather.values()) {
  println(weather.name) // SUNNY CLOUDY RAINY
}

enumValues and enumValueOf ()-Unlike java

println(enumValueOf<Weather>("sunny".toUpperCase())) // SUNNY
print(enumValues<Weather>().joinToString { it.name }) // SUNNY, CLOUDY, RAINY

Each Enum constant can also declare its own anonymous class-unlike java

Realized with kotlin
fun main(args: Array<String>) {
    println(Weather.SUNNY.washingIndexName()) //Very dry
    println(Weather.SUNNY.washingIndex()) // 5
}

enum class Weather(val weather: String) {
  CLOUDY("cloudy"){
      override fun washingIndex() = 3
      override fun washingIndexName() = "Dry"
  },
  RAINY("rainy"){
      override fun washingIndex() = 1
      override fun washingIndexName() = "Room drying recommended"
  },
  SUNNY("sunny"){
      override fun washingIndex() = 5
      override fun washingIndexName() = "Very dry"
  };
  
  abstract fun washingIndex(): Int
  abstract fun washingIndexName(): String
}

Code confirmation: https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ

Main.kt:19:4: error: expecting ';' after the last enum entry or '}' to close enum class body
  }
Realized with java
import java.util.*;

enum Weather {
    SUNNY("sunny", 5, "Very dry"),
    CLOUDY("cloudy", 3, "Dry"),
    RAINY("rainy", 1, "Room drying recommended");
    
    private String value;
    private int index;
    private String name;
    
    Weather(String value, int index, String name) {
        this.value = value;
        this.index = index;
        this.name = name;
    }
    
    public String getValue() {
        return this.value;
    }
    
    public static int getWashingIndex(Weather weather) {
        for(Weather e : Weather.values()) {
            if (weather == e) {
                return e.index;
            }
        }
        return 0;
    }
    
        public static String getWashingIndexName(Weather weather) {
        for(Weather e : Weather.values()) {
            if (weather == e) {
                return e.name;
            }
        }
        return "None";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Weather.getWashingIndexName(Weather.SUNNY)); //Very dry
        System.out.println(Weather.getWashingIndex(Weather.SUNNY)); // 5
    }
}

Code confirmation: https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ

Extension function

fun Weather.umbrellaIndex(){
    val index = when(this){
        Weather.SUNNY -> 0
        Weather.CLOUDY -> 1
        Weather.RAINY -> 2
    }
    println(index)
}

fun main(args: Array<String>) {
    Weather.RAINY.umbrellaIndex(); // 2
}

reference

Recommended Posts

Touching kotlin for the first time-Enum Classes
I tried touching Docker for the first time
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
Spring AOP for the first time
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
I tried using Docker for the first time
Walls hit by Rspec for the first time
Android Studio development for the first time (for beginners)
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
[First post] Output the characters Sold Out for sold products
Learning memo when learning Java for the first time (personal learning memo)
A summary of what Java programmers find when reading Kotlin source for the first time
[Rails] I tried using the button_to method for the first time
Beginner-friendly Android app publishing procedure, struggling for the first releaseāˆ
Modeling a Digimon with DDD for the first time Part 1
Technical causes and countermeasures for the points I was addicted to with the first Android app & Kotlin