์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ๋ฒค์๋ฏผํ๋ญํด๋ฆฐ
- recyclerview
- ์ฅ์๋ช ์ธ
- DART
- 1์ธ๊ฐ๋ฐ์
- Firebase
- ๋ช ์ธ๋ชจ์
- Flutter
- ์ข์๊ธํ์ฌํ๊ธฐ
- ๊ณต์๋ช ์ธ
- ๊ณต๋ถ์ง์ค
- Streaming
- jetpack compose
- ASMR
- ๋ช ์ฌ๋ณด๊ฐ
- androidx
- FSM
- ์ด๋ชจ์ง
- ํ์ด์ฌ
- Android
- ๋ทํ๋ฆญ์ค
- Freesound
- Linux
- ์ฝํ๋ฆฐ
- Coroutine
- ์ด๋ชจ์ง๋ฉ๋ชจ
- kotlin
- ์์ธ์นผ๋ฆฌ๋ฒ6
- ์คํ์์ค
- bash
Archives
- Today
- Total
Vintage appMaker์ Tech Blog
[kotlin] sealed class์ ๋์ฒดํฌ๋ฅผ ์ํ Elvis ์ฐ์ฐ์ ๋ณธ๋ฌธ
Source code or Tip/Android(Java, Kotlin)
[kotlin] sealed class์ ๋์ฒดํฌ๋ฅผ ์ํ Elvis ์ฐ์ฐ์
VintageappMaker 2021. 1. 6. 12:22๐ค sealed class๋ "๋ค์ํ ๋ฐ์ดํฐํ์ ์๋ฃ๋ฅผ ํ ๊ฐ์ list๋ก ๊ด๋ฆฌํด์ผํ ๋" ์ธ๋ชจ์๋ ๋ฐ์ดํฐ ํ์ด๋ค. ๊ฐ ๋ฐ์ดํฐํ์ sealed class๋ฅผ ์์๋ฐ์ผ๋ฉด ๋๋ค. ์ด ์๋ฃํ์ด Android์์ ์์ฃผ ์ฌ์ฉ๋๋ ๊ณณ์ ๋ฆฌ์ฌ์ดํด๋ฌ ๋ทฐ์ด๋ค. ํ ๋ฆฌ์คํธ์์ ํ์ ์ ๋ฐ๋ผ ๋ค์ํ ํ๋ฉด์ ๋ณด์ฌ์ฃผ์ด์ผ ํ๊ธฐ ๋๋ฌธ์ด๋ค.
๐ค Elvis ์ฐ์ฐ์ ( ?: ์๊ธด๋ชจ์์ด ์๋น์คํ๋ ์ฌ๋ฆฌ ํค์ด์คํ์ผ๊ณผ ๊ฐ๋ค๊ณ ๋ช ๋ช ๋จ)๋ ๋์ฒดํฌ๋ฅผ ์ฌ์ฉํ ๋ ์์ฃผ ์ฌ์ฉ๋๋ ์ฐ์ฐ์์ด๋ค.
package others
fun main(args: Array<String>) {
val lst = listOf<Note>(
StudyNote("์ธ๋ฌธํ", "๋ฐฐ์์ผํจ"),
WorkNote(1, "2019.07.08", "๋ฒ๊ทธ๋ถ์", "snake"),
Doodle("๊ทธ๋ฅ๋์์", "๋ง๊ตฌ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๋ค".toByteArray() )
)
// sealed class๋ ํต์ ํ๋กํ ์ฝ ๋๋ RecyclerView์ ๋ค์ํ ๋ฐ์ดํฐ์ ์ฌ์ฉ๋จ
lst.forEachIndexed { index, note ->
println("[$index]------------------------")
when(note){
is StudyNote -> {println ("๊ณต๋ถ๋
ธํธ: ${note.title}\n${note.content}")}
is WorkNote -> {println ("์
๋ฌด๋
ธํธ: ${note.number}:${note.title}\n${note.author}")}
is Doodle -> {println ("๋์๋
ธํธ: ${note.memo}\n${note.Drawing.toList()} ")}
else -> {}
}
println("---------------------------\n")
}
// ?: ์ฐ์ฐ์๋ ๋์ฑํฌ๋ฅผ ๊ฐ๋จํ๊ฒ ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํจ
var nCheck : String? = null
println(nCheck ?: "null")
println(nCheck ?: return)
println("์ถ๋ ฅ์๋จ")
}
sealed class Note
data class StudyNote(var title : String, var content : String) : Note()
data class WorkNote(val number : Int, val date : String, var title : String, val author :String) : Note()
data class Doodle (var memo : String, var Drawing : ByteArray) : Note()
์คํ๊ฒฐ๊ณผ
[0]------------------------
๊ณต๋ถ๋
ธํธ: ์ธ๋ฌธํ
๋ฐฐ์์ผํจ
---------------------------
[1]------------------------
์
๋ฌด๋
ธํธ: 1:๋ฒ๊ทธ๋ถ์
snake
---------------------------
[2]------------------------
๋์๋
ธํธ: ๊ทธ๋ฅ๋์์
[-21, -89, -120, -22, -75, -84, 32, -22, -73, -72, -21, -90, -68, -22, -73, -72, -21, -90, -84, -21, -117, -92]
---------------------------
null
'Source code or Tip > Android(Java, Kotlin)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์คํ์์ค] ๊ตฌ๊ธํ๋ ์ด ์ฐ์๊ต์ก์ฑ (0) | 2021.01.15 |
---|---|
[Android Deeplink] ์น์์ ์ฑ์ ์ค์น ๋ฐ ์คํ์ํค๊ธฐ (0) | 2021.01.11 |
Newsletter - kotlin, Android (0) | 2021.01.01 |
[kotlin] associate๋ฅผ ์ด์ฉํ list โ Map (0) | 2020.12.29 |
Android ์ฑ์ ์ฟ ํกํํธ๋ ๋ฐฐ๋๋ฌ๊ธฐ (0) | 2020.12.25 |
Comments