LoginSignup
9
5

More than 3 years have passed since last update.

初めてkotlinを触る - Enum Classes

Last updated at Posted at 2019-07-29

概要

  • 基礎
  • enumValuesとenumValueOf()
  • 各Enum定数はそれ自身の無名クラスを宣言することもできる
    • javaより簡単、便利
  • 拡張関数

内容

基本定義 - javaと同じ

enum class Weather {
  SUNNY, CLOUDY, RAINY
}

初期化 - javaと違い

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

nameとordinal - javaと同じ

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

valueOf()とvalues() - javaと同じ

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

enumValuesとenumValueOf() - javaと違い

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

各Enum定数はそれ自身の無名クラスを宣言することもできる - javaと違い

kotlinで実現
fun main(args: Array<String>) {
    println(Weather.SUNNY.washingIndexName()) // 大変よく乾く
    println(Weather.SUNNY.washingIndex()) // 5
}

enum class Weather(val weather: String) {
  CLOUDY("cloudy"){
      override fun washingIndex() = 3
      override fun washingIndexName() = "乾く"
  },
  RAINY("rainy"){
      override fun washingIndex() = 1
      override fun washingIndexName() = "部屋干し推奨"
  },
  SUNNY("sunny"){
      override fun washingIndex() = 5
      override fun washingIndexName() = "大変よく乾く"
  };

  abstract fun washingIndex(): Int
  abstract fun washingIndexName(): String
}

コード確認: https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ

  • 注意: 最後は;が必要、なければ以下のエラーになってしまう
Main.kt:19:4: error: expecting ';' after the last enum entry or '}' to close enum class body
  }
javaで実現
import java.util.*;

enum Weather {
    SUNNY("sunny", 5, "大変よく乾く"),
    CLOUDY("cloudy", 3, "乾く"),
    RAINY("rainy", 1, "部屋干し推奨");

    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 "なし";
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Weather.getWashingIndexName(Weather.SUNNY)); // 大変よく乾く
        System.out.println(Weather.getWashingIndex(Weather.SUNNY)); // 5
    }
}

コード確認:https://paiza.io/projects/DANyJNSE2ziwn6fqbmLzoQ

拡張関数

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
}

参照

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5