목록Mobile (128)
Hanbit the Developer
Kotlin Documentation 시리즈에 대해 Category: Concepts - Functions 문서 링크: https://kotlinlang.org/docs/functions.html Function usage Variable number of arguments (varargs) fun asList(vararg ts: T): List { val result = ArrayList() for (t in ts) // ts is an Array result.add(t) return result } val list1 = asList(1, 2, 3) // Spread operator val a = arrayOf(1, 2, 3) val list2 = asList(-1, 0, *a, 4) Infix not..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/type-aliases.html 기존에 존재하는 타입의 alternative name을 제공한다. typealias NodeSet = Set typealias FileTable = MutableMap typealias MyHandler = (Int, String, Any) -> Unit typealias Predicate = (T) -> Boolean class A { inner class Inner } class B { inner class Inner } typealias AInner = A.Inner typealia..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/delegated-properties.html Delegated properties val/var : by class Example { var p: String by Delegate() } class Delegate { operator fun getValue(thisRef: Any?, property: KProperty): String { return "$thisRef, thank you for delegating '${property.name}' to me!" } operator fun setValue(thisRef:..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/delegation.html interface Base { fun print() } class BaseImpl(val x: Int) : Base { override fun print() { print(x) } } class Derived(b: Base) : Base by b fun main() { val b = BaseImpl(10) Derived(b).print() } BaseImpl에서 구현된 print()를 Derived에서 그대로 사용할 수 있다.(구현 내용을 다른 클래스에 위임함) Overriding a mem..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/object-declarations.html Object expressions 객체를 일시적으로 사용할 때 유용하다: val helloWorld = object { val hello = "Hello" val world = "World" // object expressions extend Any, so `override` is required on `toString()` override fun toString() = "$hello $world" } Inheriting anonymous objects from subtype..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/inline-classes.html Backgrounds class Password(private val s: String) 위처럼 데이터가 한 개가 필요하지만 클래스로 wrap해야 하는 경우가 있다. 위처럼 하면 힙에 인스턴스를 추가해야 하기 때문에 오버헤드가 심하다.(primitive type일 경우에는 특히 더 심하다.) Usage value class Password(private val s: String) // To declare an inline class for the JVM backend @JvmInlin..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/enum-classes.html enum constant는 오브젝트이며 컨마로 구분한다. enum class Direction { NORTH, SOUTH, WEST, EAST } 각각의 enum은 enum 클래스의 인스턴스이다. enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } Anonymous classes enum class ProtocolState { WAITING { override fun signal() = TALK..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/nested-classes.html Nested class 논리적으로 연관된 클래스를 그루핑하는 데 쓰인다. class Outer { private val outer = "Outer" class InnerClass { init { print(outer) // ERROR } } } val demo = Outer.InnerClass() 자바의 static class InnerClass 와 같다. Inner class outer class의 멤버에 접근하여 outer class와 상호작용할 수 있는 클래스이다. class P..
![](http://i1.daumcdn.net/thumb/C150x150/?fname=https://blog.kakaocdn.net/dn/bVab8P/btsg1eI9OWn/Ab3epWSo27sqRxusKt8i60/img.png)
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/generics.html Variance: in, out 배경: String은 Object의 하위 타입이지만, MutableList은 MutableList의 서브클래스가 아니기 때문에 기존의 generic을 통한 객체지향 프로그래밍 설계가 힘듦 Example List는 covariance(public interface List)이기 때문에 정상 작동함 open class Animal class Cat : Animal() class Dog : Animal() fun myAnimals(animals: List) { prin..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/sealed-classes.html sealed 클래스의 서브클래스는 컴파일 타임에 알려져있으며, sealed class가 정의된 곳이 아닌, 다른 모듈이나 다른 패키지에서 또다른 서브클래스가 정의될 수 없다. sealed interface는 다른 모듈에서만 재정의가 불가능하다.(모듈이 컴파일되면 새 구현체가 나타날 수 없다.) enum 또한 값들의 집합이 제한되어 있다는 점이 있어 유사하다. 하지만 enum의 상수는 single instance로서 존재하는 반면, sealed class의 서브클래스는 여러 인스턴스를 ..