목록Mobile (128)
Hanbit the Developer
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/data-classes.html 컴파일러가 자동으로 다음 메소드들을 추가한다: equals() hashCode() toString() → “User(name=John, age=42)” componentN() copy() cannot be abstract, open, sealed, or inner Properties declared in the class body data class Person(val name: String) { var age: Int = 0 } val person1 = Person("John") val..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/extensions.html 데코레이터 패턴, 상속 없이 클래스, 인터페이스를 확장할 수 있다: fun MutableList.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } Extensions are resolved statically 멤버를 추가하는 것이 아니라, statically하게 작동한다.(런타임 때의 타입으로 작..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/visibility-modifiers.html Packages 함수, 프로퍼티, 클래스, 오브젝트, 인터페이스는 패키지 내에서 top-level단에 선언될 수 있다: // file name: example.kt package foo fun baz() { ... } class Bar { ... } private: 해당 파일에서만 보임 internal: 같은 모듈에서만 보임 public Class members private: 해당 클래스에서만 보임 protected: private와 같으나 subclasses에는 공개됨 ..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/fun-interfaces.html Functional interface or Single Abstract Method (SAM) interface non-abstract members를 여러 개 가질 수 있으나 abstract member는 1개만 가질 수 있다: fun interface OnClickListener { fun onClick(i: Int): Boolean } SAM conversions without a SAM conversion // Creating an instance of a class val i..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/interfaces.html interface Named { val name: String } interface Person : Named { val firstName: String val lastName: String override val name: String get() = "$firstName $lastName" } data class Employee( // implementing 'name' is not required override val firstName: String, override val lastNa..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/properties.html Getters and setters var [: ] [= ] [] [] var stringRepresentation: String = "Init" get() = this.toString() set(value) { setDataFromString(value) // parses the string and assigns values to other properties } setter의 visibility modifier 정의: var setterVisibility: String = "abc" pr..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/inheritance.html Any 모든 클래스는 Any를 상속함 equals() hashCode() toString() Open open: 클래스를 상속하기 위해 사용한다. open class Base(p: Int) class Derived(p: Int) : Base(p) open class Shape { open fun draw() { /*...*/ } fun fill() { /*...*/ } } class Circle() : Shape() { override fun draw() { /*...*/ } } final..
Kotlin Documentation 시리즈에 대해 Category: Concepts - Classes and objects 문서 링크: https://kotlinlang.org/docs/classes.html Constructor class Person constructor(firstName: String) { /*...*/ } 생성자에 어노테이션(e.g. class A @Inject constructor(…))이나 visibility modifiers가 없다면 constructor 키워드를 생략해도 된다: class Person(firstName: String) { /*...*/ } init class InitOrderDemo(name: String) { val firstProperty = "Firs..
Kotlin Documentation 시리즈에 대해 Category: Concepts 문서 링크: https://kotlinlang.org/docs/reflection.html 리플랙션은 런타임에서 프로그램의 구조를 살펴볼 수 있는 기능을 제공한다. functional or reactive style을 사용할 때, Kotlin의 일급 객체(first-class citizens, 변수나 데이터에 저장될 수 있고 함수 인자로 넘길 수 있으며 함수의 결과로서 반환될 수 있는 특징이 있다.)인 함수와 프로퍼티를 관찰해야 할 필요성이 있다. JVM dependency 리플랙션을 사용하기 위해 의존성을 추가해야 한다. dependencies { implementation("org.jetbrains.kotlin:ko..
Kotlin Documentation 시리즈에 대해 Category: Concepts 문서 링크: https://kotlinlang.org/docs/destructuring-declarations.html val (name, age) = person 위 코드는 아래 코드로 컴파일된다. val name = person.component1() val age = person.component2() for문에서도 적용된다. for ((key, value) in map) { // do something with the key and the value } Underscore for unused variables val (_, status) = getResult() Destructuring in lambdas { a..