Created
April 19, 2018 22:47
-
-
Save mapyo/0351987ef26fbe0a587b00afb17eeee1 to your computer and use it in GitHub Desktop.
seald class sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.hoge | |
import org.junit.Test | |
sealed class Event { | |
class Loading : Event() | |
class Loaded(val result: String) : Event() | |
class LoadFailed(val reason: String) : Event() | |
} | |
class SampleTest { | |
@Test | |
fun sample() { | |
val event1 = Event.Loading() | |
val event2 = Event.Loaded("1です") | |
val event3 = Event.LoadFailed("やんごとなき理由です") | |
val events = listOf(event1, event2, event3) | |
events.forEach { hoge(it) } | |
} | |
fun hoge(event: Event) { | |
when (event) { | |
is Event.Loading -> println("読み込み中です") | |
is Event.Loaded -> println("こんな感じの結果です ${event.result}") | |
is Event.LoadFailed -> println("残念ですが失敗しました ${event.reason}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment