SlideShare a Scribd company logo
1 of 13
Download to read offline
Property
Pluu
Kotlin에는
Field가 없다
그렇다고 하네요
Grammar
property (used by memberDeclaration, declaration, topLevelObject)
: modifiers ("val" | "var")
typeParameters?
(type ".")?
(multipleVariableDeclarations | variableDeclarationEntry)
typeConstraints
("by" | "=" expression SEMI?)?
(getter? setter? | setter? getter?) SEMI?
https://kotlinlang.org/docs/reference/grammar.html#property
어려우니… 쉬운걸 봅시다
Grammar
var <propertyName>[: <PropertyType>] [=
<property_initializer>]
[<getter>]
[<setter>]
https://kotlinlang.org/docs/reference/properties.html
Options … ...
● <property_initializer>
● <getter> ………………………………………………. Default Getter
● <setter> ………………………………………………. Default Setter
● <PropertyType>
○ 추론이 가능한 경우 생략 가능
○ 초기화 값을 통해
○ Kotlin 1.1, getter에서 추론되는 경우 생략 가능
프로퍼티가 해주는 것
● Filed와 Accessor 자동 생성
● Property Accessor 를 Accessor 호출로 변환
● lombok과 비슷
○ 직접 접근하지만 암묵적으로 Accessor를 경우해서 호출
Backing ???
사실… 잘 모르겠어요
Accessor
Product().id
class Product {
var id = 0
get() = field / 10
set(value) {
field = value * 10
}
}
Accessor
Backing Field
var counter = 0
get() = field
set(value) {
if (value >= 0) counter = value
}
StackOverFlow
Backing Field
var counter = 0
get() = field
set(value) {
if (value >= 0) field = value
}
Backing Property
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null) {
_table = HashMap()
}
return _table ?: throw AssertionError("Set to null by another thread")
}
끝!

More Related Content

Similar to 프...프로퍼티

[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기Sang Heon Lee
 
Domain Specific Languages With Groovy
Domain Specific Languages With GroovyDomain Specific Languages With Groovy
Domain Specific Languages With GroovyTommy C. Kang
 
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It종빈 오
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?NAVER D2
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, ScalabilityDongwook Lee
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
 

Similar to 프...프로퍼티 (8)

[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
 
Domain Specific Languages With Groovy
Domain Specific Languages With GroovyDomain Specific Languages With Groovy
Domain Specific Languages With Groovy
 
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It
[WELC] 22. I Need to Change a Monster Method and I Can’t Write Tests for It
 
[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?[143] Modern C++ 무조건 써야 해?
[143] Modern C++ 무조건 써야 해?
 
Scalability
ScalabilityScalability
Scalability
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, Scalability
 
Linq
LinqLinq
Linq
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 

프...프로퍼티