Hanbit the Developer
Kotlin Documentation | Data classes 본문
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 person2 = Person("John")
person1.age = 10
person2.age = 20
다만 name에 대한 component1() 함수만 생성됨
Data classes and destructing declarations
val jane = User("Jane", 35)
val (name, age) = jane
println("$name, $age years of age") // prints "Jane, 35 years of age"
'Kotlin' 카테고리의 다른 글
Kotlin Documentation | Generics: in, out, where (0) | 2023.05.23 |
---|---|
Kotlin Documentation | Sealed classes and interfaces (0) | 2023.05.23 |
Kotlin Documentation | Extensions (0) | 2023.05.22 |
Kotlin Documentation | Visibility modifiers (0) | 2023.05.22 |
Kotlin Documentation | Functional (SAM) interfaces (0) | 2023.05.22 |