Kotlin

    Android Kotlin - DIP(Dependency Inversion Principle)이란?

    Android Kotlin - DIP(Dependency Inversion Principle)이란?

    DIP (Dependency Inversion Principle) → 의존 역전의 원칙 의존 관계를 맺을을 때 변하기 쉬운 것에 의존하기 보다는 변화하지 않는 것에 의존하라! 위의 말은 어떤 의미가 담겨 있을까요? 예를 들어 저희가 쇼핑물에서 사용자가 물건을 구입하면 구입한 내역을 알려주기 위해 알림 기능을 구현한다고 생각해 봅시다. 이메일로 사용자에게 알려주기로 결정을 했고 아래와 같이 기능을 구현 했습니다 class EmailMessenger { fun sendNotification(message: String): String{ println("Sending email: $message") } } class NotificationService { private val messenger = EmailM..

    Android - Kotlin으로 RecyclerView 구현하기

    Android - Kotlin으로 RecyclerView 구현하기

    build.gradle(Moudle) viewBinding true 설정 activity_main.xml item_recycler.xml 아래와 같이 데이터 클래스를 만들어 줍니다. Memo.kt data class Memo(var no: Int, var title: String, var timestamp: Long) AdapterClass도 만들어 줍니다 CustomAdapter.kt import android.view.LayoutInflater import android.view.ViewGroup import android.widget.Toast import androidx.recyclerview.widget.RecyclerView import org.techtown.location.kotlinpro..

    [Kotlin] - 코틀린 기본 문법 4

    [Kotlin] - 코틀린 기본 문법 4

    fun fun double(x: Int): Int{ return 2 * x } 함수 정의는 fun 키워드를 사용한다. 함수 사용 val result = double(2) 함수 호출 Stream().read() 함수 파라미터는 Pascal notation 을 써서 정의한다. 각 파라미터는 type 이 정의되어야 한다. fun powerOf(number: Int, exponent: Int) { /*...*/ } 파라미터 함수 파라미터는 argument 가 생략 된 경우에 할당되는 해당 파라미터의 기본 값을 가질 수 있다. fun read(b: Array, off: Int = 0, len: Int = b.size) { /*...*/ } 본 값은 type 과 값 사이에 = 를 사용하여 정의한다. overridi..

    [Kotlin] - 코틀린 기본 문법 3

    [Kotlin] - 코틀린 기본 문법 3

    불변형 List 생성하기 fun main(array: Array){ var numbers: List = listOf(1,2,3,4,5) var names: List = listOf("one","two","three") var mixedType = listOf("Hello", 1, 2.45, 's') // 타입 for(number in numbers) print(number) // 12345 println() for(index in names.indices) println("names[$index] = ${names[index]}") // names[0] = one for (index in names.indices) // 짝수 요소만 if(index % 2 == 0) println("names[$index..

    [Kotlin] - 코틀린 기본 문법 2

    [Kotlin] - 코틀린 기본 문법 2

    기본 문법2 (?:, ?., as?, !!, lateinit) 엘비스 ?: 연산자 fun main(args: Array){ var fishFood : Int? = null fishFood = fishFood?.dec() ?: 0 println(fishFood) } fun main(args: Array){ var yts: String? = null fun elvis() { val name: String = yts ?: "YTS" val nameTwo: String = yts ?: return //함수자체를 return 시키도록 만들 수 도 있음 val nameThree: String = yts ?: throw NullPointerException() } } 엘비스 연산자는 널 값을 허용하지 않는 변수에 널..

    [Kotlin] - 코틀린 기본 문법 1

    [Kotlin] - 코틀린 기본 문법 1

    코틀린 기본 문법 reference by Kotlin 공식 문서 Kotlin 구조 package kotlinproject fun main(){ println("Hello World") } .kt 확장자를 가진다. package, import 선언부는 자바와 동일 함수의 fun 키워드로 시작한다. 리턴 타입은 함수명() 뒤쪽에 명시 코틀린은 문장의 끝을 세미콜론으로 명시하지 않음 변수 타입, 정의 및 초기화 데이터 타입에 관계 없는 상수는 val, 변수는 var 로 선언 데이터 타입을 명시하지 않아도 할당 해주는 값에 따라 자동으로 결정됨 val는 자바와 final과 같으므로 다른 값으로 재 할당할 경우 위와 같이 오류가 난다. 위의 경우는 var 키워드로 num 변수를 int 형으로 타입을 정하였는데, ..