kotlin
[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] 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,..
[Kotlin] 클래스 toString/equals/hashCode, data class
클래스 - toString val product = Product("패션", "운동화") println("$product") // Product@60addb54 -> 인스턴스 정보를 얻는데 도움X class Product(val categoryLabel: String, val name: String=""){ override fun toString(): String { return "Product(categoryLabel=$categoryLabel, name=$name)" } } 클래스 - equals val prodcut1 = Product("패션", "운동화") val prodcut1 = Product("패션", "운동화") println(product1 == product2) // false - 같은..
[Kotlin] 함수, Superclass, Subclass
함수 1) 함수의 body 가 block // 반환 타입 없음 fun start(){ println("Hello World!") } // 반환 타입 있음 fun validString(string: String?): Boolean { return !string.isNullOrBlank() } 2) 함수 body가 식 (Single expression) fun double(x: Int): Int = x * 2 // 반환 타입 생략 가능 fun double(x: Int) = x * 2 3) top-level 함수 - 클래스 안에 함수를 넣지 않아도 됨 - main 함수 4) 클래스 멤버 함수 - 클래스 내부에 정의 fun main(args: Array){ val home = Home() home.start()..
[Kotlin] 클래스 상속, 추상화, 인터페이스
객체 - 객체는 식별자(identity), 상태(state), 행동(behavior) 을 가진 실체 클래스 class Product // 필수 파라미터 지정 class Product constructor(val categoryLabe: String) // constructor 생성자가 한 개 있을 시 생략 가능 class Product(val categoryLabel: String) // constructor 생성자가 두 개 이상 일 시 (주 생성자, 부 생성자) class Product(val categoryLabel: String) { constructor(categoryLabel: String, name: String): this(categoryLabel) } // 파라미터에 디폴트 값이 존재한다면..
[Kotlin] Operator, conditions & loop
Operator - Safe calls ?. : safe call operator - nullable 타입 변수 속성에 접근 가능 var userName: String? = null if(userName.isEmpty() // Error : userName이 null이므로 isEmpty() 호출 불가능 { println("사용자 이름을 다시 입력해주세요.") } if(userName?.isEmpty() == true) { println("사용자 이름을 다시 입력해주세요.") } if(userName?.isNullOrEmpty()) // 공백을 여러 번 입력하는 경우 처리가 되지 않음 println("사용자 이름을 다시 입력해주세요.") } if(userName?.isNullOrBlank()) // 빈 문..