목록Kotlin (26)
Foggy day
코루틴의 순차적, 동시적 작업들을 컨트롤 할 수 있게 해주는 것이 Defferd 객체인데 이는 JOB을 상속받았다. 1. 코루틴 순차적 실행 class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) //코루틴은 비동기일지라도 순차적으로 실행된다. //예를 들어 firstFunction과 secondFunction 함수가 retrofit 호출일지라도 //먼저 실행한 firstFunction 함수의 콜백이 도달해서 완전..
runBlocking { // Dispatchers.Default 는 코루틴이 GlobalScope 에서 실행될 경우에 // 사용되며 공통으로 사용되는 백그라운드 스레드 풀을 이용합니다. // 즉, launch(Dispatchers.Default) {…} 와 GlobalScope.launch {…} 는 // 동일한 디스패처를 사용합니다. //코루틴을 cancel시키기 위해서는 suspend function call 이 필요함 //yield와 delay()가 suspend function 역할을 함 val startTime = System.currentTimeMillis() val job = launch(Dispatchers.Default) { try { var nextPrintTime = startTim..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) introduce() hungry.start() age() //GlobalScope.launch는 thread와 비슷한 기능 // launch는 코루틴의 빌더이며 빌더를 사용하기 위해서는 scope가 필요하다 // GlobalScope는 scope중의 하나 //delay는 suspend 함수로 일시중단같은 개념, suspend 함수는 scope 안에서거나 백그..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val human = Human("Man", 30, "jerome") val (sex, age, name) = human println("sex : $sex, age : $age, name : $name") ///// sex : Man, age : 30, name : jerome ////// } } data class Human( val sex: String,..
val value = object { val x = 0 val y = 10 val z = 20 } println("value x : ${value.x}, y : ${value.y}, z : ${value.z}") //// value x : 0, y : 10, z : 20 val school: School = object : School(3), ClassGroup { override val students: Int get() = super.students override fun OnJoinClassGroup(classNumber: Int) { super.OnJoinClassGroup(3) } } println("school.students ${school.students}") //// school.stud..
val list = listOf(3, 4, 5, 6, 7, 8, 9, 10) val value = 12 val result = when (value) { in list -> "value is in the list" in 1..10 -> "It is in the range A" in 10..20 -> "It is in the range B" else -> "none fo the above" } println("result : $result") //result : It is in the range B val array = arrayOf("A", "B", "C", "D") for (i in array.indices) { println("i : ${array[i]}") } // A, B, C, D for ((i..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val lists = listOf('a', 'f', 'd', 'w') println("toString : ${toString(lists)}") println("add : ${add(10, 15)}") println("addSecond : ${addSecond(10, 15)}") println("factorialFirst : ${Factorial.factoria..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val lists = listOf('a', 'b', 'c', 'd', 'e') println("toString : ${toString(lists, "text")}") println("toStringSecond : ${toStringSecond(lists, "text")}") println("toStringThird : ${toStringThird(lists, ..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val toothPaste = Product("ToothPast", Price(2.0), Weight(3.1)) val toothBrush = Product("ToothBrush", Price(3.0), Weight(2.5)) val orderLines = listOf( OrderLine(toothPaste, 3), OrderLine(toothBrush, 5)..
class KotlinPlayGroundActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_kotlin_play_ground) val a = "A" val b = "B" val c = "C" val d = "D" println("func : ${func(a, b, c, d)}") println("curriedFunc : ${curriedFunc()(b)(c)(d)(a)}") println("curriedString : ${curriedString(a)(b)(c)(d)}") } fun ..