Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 설계
- zuul
- spring cloud netflix eureka
- code refactoring
- BFS
- Spring Data Redis
- reactive
- test
- 서비스스펙
- java #jvm #reference #gc #strong reference
- 단위테스트
- netflix
- container image #docker #layer #filesystem #content addressable
- springcloud
- 탐색
- unittest
- spring cloud
- Java
- Dynamic Routing
- netflix eureka
- Eureka
- microservice architecture
- spring cloud netflix
- forkandjoinpool #threadpool #jvm #async #non-blocking
- dfs
- api-gateway
- docker
- spring cloud netflix zuul
- unit
Archives
- Today
- Total
phantasmicmeans 기술 블로그
7. Kotlin - Properties 본문
Kotlin Properties and Fields
코틀린 class의 프로퍼티들은 var
or val
키워드로 선언된다. 이 프로퍼티들은 default로 접근자(get, set) method를 포함하기에 코틀린에서의 프로퍼티
란 필드와 접근자를 통칭하는 개념이다.
class Address {
var name: String = "Holmes, Sherlock"
var street: String = "Baker"
var city: String = "London"
var state: String? = null
var zip: String = "123456"
}
프로퍼티는 아래와 같이 간단하게 프로퍼티 이름으로 접근할 수 있다.
fun copyAddress(address: Address): Address {
val result = Address() // there's no 'new' keyword in Kotlin
result.name = address.name // accessors are called
result.street = address.street
// ...
return result
}
Getters and Setters
코틀린에서의 프로퍼티는 필드와 접근자를 통칭하기에 full syntax는 아래와 같다.
- getter, setter는 생략 가능
var <propertyName>[: <PropertyType>] [= <property_initializer>]
[<getter>]
[<setter>]
명세를 보고 아래 프로퍼티들을 보면 접근자 (getter, setter) 를 default로 포함한다는 것을 알 수 있다.
class Test {
var initialized = 1 // has type Int, default getter and setter
val inferredType = 1 // has type Int and a default getter
}
접근자를 customize 할 수 있는데 예시는 아래와 같다.
class Test {
var list = mutableListOf(1,2,3,4,5)
val isEmpty: Boolean
get() = this.list.size == 0
var str: String = ""
get() = this.list[0].toString()
set(value) {
if (field.length > value.length) {
field = value
}
field = "A"
}
}
'Programming > Kotlin' 카테고리의 다른 글
9. Kotlin - Interface (0) | 2021.01.22 |
---|---|
8. Kotlin - Backing Field (0) | 2021.01.22 |
6. Kotlin - Class Inheritance (0) | 2021.01.22 |
5. Kotlin - Class (0) | 2021.01.22 |
4. Kotlin - Return and Jumps (0) | 2021.01.22 |
Comments