Android/Kotlin
[코틀린 인 액션] 2장 정리
2.1.3 변수 기본적으로 모든 변수를 val 키워드를 사용해 불변 변수로 선언할 것, 나중에 꼭 필요할 때만 var로 변경 초기화 식을 사용하지 않고 변수를 선언하려면 변수 타입을 명시 val answer: Int answer = 42 3. val 참조 객체는 불변일지라도 그 참조가 가르키는 객체의 내부 값은 변경될 수 있음 val languages = arrayListOf("Java") languages.add("Kotlin") 2.2.2 커스텀 접근자 1. 프로퍼티 게터 선언을 통해 클라이언트가 프로퍼티에 접근할 때마다 게터가 프로퍼티 값을 매번 다시 계산 fun main(args: Array) { val rect = Rectangle(3,3) val rect2 = Rectangle(3,5) pri..
[Kotlin] enum, sealed class
enum class - 같은 타입의 여러 상수 정의 - comma로 구분 enum class Color{ RED, GREEN, BLUE } enum class + When expression - else branch 없이도 모든 조건 평가 val colorCode = when(color){ Color.RED -> "#FF0000" Color.GREEN -> "#00FF00" Color.BLUE -> "#0000FF" } 1. 생성자, 프로퍼티 선언 가능 enum class Color(val rgb: Int){ RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } 2. 함수 선언 가능 enum class ProtocolState{ WAITING{ override fun s..
[Kotlin] 고차 함수, 람다, Scope functions
Functions types 1. (parameter) -> return type 파라미터 타입 선언 -> 리턴 타입 선언 (Int) -> Boolean (input: Int) -> Boolean (T) -> Boolean val isEven: (Int) -> Boolean = { it % 2 == 8 } 파라미터가 두개 이상일 때는 다음과 같다 (Int, Int) -> Int 파라미터명과 타입을 함께 작성할 수도 있다 (first: Int, second: Int) -> Int 2. with receiver - 함수 본문에 receiver 객체 참조 전달 .()는 this를 통해 참조(생략 가능) Int.() -> Boolean T.() -> R (마지막 줄 반환) val isEven: Int.() ->..
[Kotiln] Nested & inner class, Extension함수, Generics
Nested class / interface 1. 중첩된 클래스 class Outer { private val bar: Int = 1 class Nested { fun foo() = 2 } } val demo = Outer.Nested().foo() // == 2 Nested 클래스에서는 Outer 클래스의 프로퍼티나 함수에 접근 불가능 2. 중첩된 인터페이스 - 클래스 내부에 클래스나 인터페이스를 중첩하여 작성 가능 interface OuterInterface{ class InnterClass interface InnerInterface } class OuterClass{ class InnterClass interface InnterInterface } inner class 1. Outer 클래스의 멤..
[Kotlin] object
object 선언 object CartItems { private val mutableProducts = mutableListOf() val products: List = mutableProducts fun addProduct(product: Product){ mutableProducts.add(product) } } 싱글턴 패턴 - 전체에서 단일 객체로 유지하는 기법 - 인스턴스를 하나만 만들어 사용 object exrpessions - 무명 객체 1. 무명 객체 생성 - 이름을 생략하고 객체의 프로퍼티와 함수를 정의 - 단일 객체로 생성되는 것과 다르게 매번 새로운 객체를 생성 val cartItems object { val products = mutableListOf(Product("전자기기","핸..
[Kotlin] List, Set, Map, Collection Operations
Collections 1. List - ordered collection - index로 원소에 접근 가능 listOf(1,2,2) --> [1,2,2] 2. Set - unique elements setOf(1,2,2) ---> [1,2] 3. - key-value pairs - 동일한 key에 대해 하나의 value만 가짐 mapOf("first" to 1, "second" to 2, "second" to 3) --> {first=1, seconde=3} 4. Type 1) read-only val numbers = listOf("one", "two", "three", "four") 2) mutable - 순서를 가지며, 변경이 가능 val numbers = mutableListOf("one", "t..
[Kotlin] Package, 가시성 변경자(public,private,internal,protected)
Package 다른 package 참조 : import shopping package의 class Cart 와 class Store 을 home package에서 참조하기 위해서는 import shopping.Cart import shopping.Store // 해당 패키지의 모든 하위 내용을 import import shopping.* class Home { val stroe = Strore() val cart = Cart() } Module - 한 번에 compile 되는 묶음 가시성 변경자 visibility modifier - 클래스, 변수, 함수를 외부에 노출하는 범위를 결정 - top-level : public, private, internal - 클래스 멤버 : public, private,..