SlideShare a Scribd company logo
Kotlin
Properties and Fields
새차원
새로운 차원의 앱을 지향합니다.
http://blog.naver.com/cenodim
hohoins@nate.com
새차원
Properties and Fields
• 프로퍼티 선언
– 코틀린 클래스는 프로퍼티를 가질 수 있음
• var (mutable) / val (read-only)
– 프로퍼티 사용은 자바의 필드를 사용하듯이 하면 됨
새차원
class Address {
var name: String = "Kotlin"
val city: String = "Seoul"
}
fun copyAddress(address: Address): Address {
val result = Address()
result.name = address.name
// ...
return result
}
Properties and Fields
• 프로퍼티 문법
– 전체 문법
– 옵션 (생략가능)
• PropertyType
– property_initializer에서 타입을 추론 가능한 경우 생략가능
• property_initializer
• getter
• setter
새차원
var <propertyName>[: <PropertyType>] [=<property_initializer>]
[<getter>]
[<setter>]
Properties and Fields
• 프로퍼티 예제
– 두 코드는 거의 같음
새차원
class Address {
var name = "Kotlin"
}
class Address2 {
var name: String = "Kotlin"
get() {
return field
}
set(value) {
field = value
}
}
Properties and Fields
• var (mutable) 프로퍼티
새차원
class Address {
// default getter와 setter
// 타입은 Int
var initialized = 1
// error: 오류 발생
// default getter와 setter를 사용한 경우
// 명시적인 초기화가 필요함
var allByDefault: Int?
}
Properties and Fields
• val (read-only) 프로퍼티
– var대신 val 키워드 사용
– setter가 없음
새차원
class Address {
// default getter
// 타입은 Int
val initialized = 1
// error: 오류 발생
// default getter
// 명시적인 초기화가 필요함
val allByDefault: Int?
}
Properties and Fields
• Custom accessors (getter, setter)
– custom accessor는 프로퍼티 선언 내부에, 일반 함수 처럼 선언
할 수 있음
– getter
– setter
• 관습적으로 setter의 파라매터의 이름은 value임 (변경가능)
새차원
val isEmpty: Boolean
get() = this.size == 0
var stringRepresentation: String
get() = this.toString()
set(value) {
setDataFromString(value)
}
Properties and Fields
• 타입생략
– 코틀린 1.1 부터는 getter를 통해 타입을 추론 할 수 있는 경우,
프로퍼티의 타입을 생략할 수 있음
새차원
val isEmpty: Boolean
get() = this.size == 0 // has type Boolean
val isEmpty
get() = this.size == 0 // has type Boolean
Properties and Fields
• 프로퍼티
– accessor에 가시성 변경이 필요하거나
– accessor에 어노테이션이 필요한 경우
– 기본 accessor의 수정 없이 body없는 accessor를 통해 정의 가능
– Body를 작성해 주어도 됨
새차원
var setterVisibility: String = "abc"
private set
var setterWithAnnotation: Any? = null
@Inject set // annotate the setter with Inject
var setterVisibility: String = "abc"
private set(value) {
field = value
}
Properties and Fields
• Backing Fields
– 코틀린 클래스는 field를 가질 수 없음
– ‘field’라는 식별자를 통해 접근할 수 있는, automatic backing
field를 제공함
– field는 프로퍼티의 accessor에서만 사용가능 함
새차원
// the initializer value is written directly
// to the backing field
var counter = 0
set(value) {
if (value >= 0) field = value
}
Properties and Fields
• Backing Fields 생성 조건
– accessor중 1개라도 기본 구현을 사용하는 경우
– custom accessor에서 field 식별자를 참조하는 경우
– 아래의 경우는 backing field를 생성하지 안음
새차원
// the initializer value is written directly to the backing field
var counter = 0
set(value) {
if (value >= 0) field = value
}
val isEmpty: Boolean
get() = this.size == 0
Properties and Fields
• Backing Properties
– “implicit backing field“ 방식이 맞지 안는 경우에는
– “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("null ")
}
Properties and Fields
• Compile-Time Constants
– const modifier를 이용하면 컴파일 타임 상수를 만들 수 있음
• 이런 프로퍼티는 어노테이이션 에서도 사용 가능함
– 조건
• Top-level 이거나
• object의 멤버이거나
• String나 프리미티브 타입으로 초기화된 경우
• custom getter이 없는 경우
새차원
const val SUBSYSTEM_DEPRECATED: String = "This
subsystem is deprecated"
@Deprecated(SUBSYSTEM_DEPRECATED)
fun foo() { ... }
Properties and Fields
• Late-Initialized Properties
– 일반적으로 프로퍼티는 non-null 타입으로 선언됨
– 간혹 non-null 타입 프로퍼티를 사용하고 싶지만, 생성자에서 초
기화를 해줄 수 없는 경우가 있음
• Dependency injection
• Butter knife
• Unit test의 setup 메소드
새차원
public class MyTest {
lateinit var subject: TestSubject
@SetUp fun setup() {
subject = TestSubject()
}
@Test fun test() {
subject.method() // dereference directly
}
}
Properties and Fields
• Late-Initialized Properties
– 객체가 constructor에서는 할당 되지 못하지만
– 여전히 non-null타입으로 사용하고 싶은 경우
– Lateinit modifier를 사용가능
– 조건
• 클래스의 바디에서 선언된 프로퍼티만 가능
• 기본생성자에서 선언된 프로퍼티는 안 됨
• var 프로퍼티만 가능
• custom accessor이 없어야 함
• non-null 타입이어야 함
• 프리미티브 타입이면 안됨
– lateinit 프로퍼티가 초기화 되기 전에 접근되면, 오류가 발생됨
• kotlin.UninitializedPropertyAccessException: lateinit property tet
has not been initialized
새차원
감사합니다.
새차원

More Related Content

What's hot

The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표
재정 이
 

What's hot (17)

Java programming pdf
Java programming pdfJava programming pdf
Java programming pdf
 
Scala type class pattern
Scala type class patternScala type class pattern
Scala type class pattern
 
Python class
Python classPython class
Python class
 
Swift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structureSwift 3 Programming for iOS : class and structure
Swift 3 Programming for iOS : class and structure
 
15 swift 클래스
15 swift 클래스15 swift 클래스
15 swift 클래스
 
17 swift 프로토콜
17 swift 프로토콜17 swift 프로토콜
17 swift 프로토콜
 
Swift3 typecasting nested_type
Swift3 typecasting nested_typeSwift3 typecasting nested_type
Swift3 typecasting nested_type
 
Scala dir processing
Scala dir processingScala dir processing
Scala dir processing
 
Swift3 subscript inheritance initialization
Swift3 subscript inheritance initializationSwift3 subscript inheritance initialization
Swift3 subscript inheritance initialization
 
[세미나] 20160819 Java 프로그래머를 위한 Scala 튜토리얼
[세미나] 20160819 Java 프로그래머를 위한 Scala 튜토리얼[세미나] 20160819 Java 프로그래머를 위한 Scala 튜토리얼
[세미나] 20160819 Java 프로그래머를 위한 Scala 튜토리얼
 
[자바카페] 자바 객체지향 프로그래밍 (2017)
[자바카페] 자바 객체지향 프로그래밍 (2017)[자바카페] 자바 객체지향 프로그래밍 (2017)
[자바카페] 자바 객체지향 프로그래밍 (2017)
 
Java script
Java scriptJava script
Java script
 
The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표The c++ programming language 10장 클래스 발표
The c++ programming language 10장 클래스 발표
 
Scala self type inheritance
Scala self type inheritanceScala self type inheritance
Scala self type inheritance
 
Swift3 generic
Swift3 genericSwift3 generic
Swift3 generic
 
스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding스칼라 클래스 이해하기 _Scala class understanding
스칼라 클래스 이해하기 _Scala class understanding
 
파이썬 Xml 이해하기
파이썬 Xml 이해하기파이썬 Xml 이해하기
파이썬 Xml 이해하기
 

Similar to [새차원, 코틀린(Kotlin) 강좌] 9. Properties and Fields

NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
Min-soo Park
 

Similar to [새차원, 코틀린(Kotlin) 강좌] 9. Properties and Fields (14)

이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
이것이 자바다 Chap.11 기본 API 클래스(java)(KOR)
 
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
NHNNEXT 개경프14 Subway Rocket Team Study 3rd C++
 
JavaScript Patterns - Chapter 3. Literals and Constructors
JavaScript Patterns - Chapter 3. Literals and ConstructorsJavaScript Patterns - Chapter 3. Literals and Constructors
JavaScript Patterns - Chapter 3. Literals and Constructors
 
Java_05 class
Java_05 classJava_05 class
Java_05 class
 
Why what how kotlin
Why what how kotlinWhy what how kotlin
Why what how kotlin
 
[Swift] Properties
[Swift] Properties[Swift] Properties
[Swift] Properties
 
Let's Go (golang)
Let's Go (golang)Let's Go (golang)
Let's Go (golang)
 
9 swift 클로저1
9 swift 클로저19 swift 클로저1
9 swift 클로저1
 
생코자바스크립트스터디3장
생코자바스크립트스터디3장생코자바스크립트스터디3장
생코자바스크립트스터디3장
 
Kotlin with fp
Kotlin with fpKotlin with fp
Kotlin with fp
 
객체지향 프로그래밍 기본
객체지향 프로그래밍 기본객체지향 프로그래밍 기본
객체지향 프로그래밍 기본
 
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
스파르탄스터디 E04 Javascript 객체지향, 함수형 프로그래밍
 
Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)Swift3 : class and struct(+property+method)
Swift3 : class and struct(+property+method)
 
Java standard(8~13)
Java standard(8~13)Java standard(8~13)
Java standard(8~13)
 

[새차원, 코틀린(Kotlin) 강좌] 9. Properties and Fields

  • 1. Kotlin Properties and Fields 새차원 새로운 차원의 앱을 지향합니다. http://blog.naver.com/cenodim hohoins@nate.com 새차원
  • 2. Properties and Fields • 프로퍼티 선언 – 코틀린 클래스는 프로퍼티를 가질 수 있음 • var (mutable) / val (read-only) – 프로퍼티 사용은 자바의 필드를 사용하듯이 하면 됨 새차원 class Address { var name: String = "Kotlin" val city: String = "Seoul" } fun copyAddress(address: Address): Address { val result = Address() result.name = address.name // ... return result }
  • 3. Properties and Fields • 프로퍼티 문법 – 전체 문법 – 옵션 (생략가능) • PropertyType – property_initializer에서 타입을 추론 가능한 경우 생략가능 • property_initializer • getter • setter 새차원 var <propertyName>[: <PropertyType>] [=<property_initializer>] [<getter>] [<setter>]
  • 4. Properties and Fields • 프로퍼티 예제 – 두 코드는 거의 같음 새차원 class Address { var name = "Kotlin" } class Address2 { var name: String = "Kotlin" get() { return field } set(value) { field = value } }
  • 5. Properties and Fields • var (mutable) 프로퍼티 새차원 class Address { // default getter와 setter // 타입은 Int var initialized = 1 // error: 오류 발생 // default getter와 setter를 사용한 경우 // 명시적인 초기화가 필요함 var allByDefault: Int? }
  • 6. Properties and Fields • val (read-only) 프로퍼티 – var대신 val 키워드 사용 – setter가 없음 새차원 class Address { // default getter // 타입은 Int val initialized = 1 // error: 오류 발생 // default getter // 명시적인 초기화가 필요함 val allByDefault: Int? }
  • 7. Properties and Fields • Custom accessors (getter, setter) – custom accessor는 프로퍼티 선언 내부에, 일반 함수 처럼 선언 할 수 있음 – getter – setter • 관습적으로 setter의 파라매터의 이름은 value임 (변경가능) 새차원 val isEmpty: Boolean get() = this.size == 0 var stringRepresentation: String get() = this.toString() set(value) { setDataFromString(value) }
  • 8. Properties and Fields • 타입생략 – 코틀린 1.1 부터는 getter를 통해 타입을 추론 할 수 있는 경우, 프로퍼티의 타입을 생략할 수 있음 새차원 val isEmpty: Boolean get() = this.size == 0 // has type Boolean val isEmpty get() = this.size == 0 // has type Boolean
  • 9. Properties and Fields • 프로퍼티 – accessor에 가시성 변경이 필요하거나 – accessor에 어노테이션이 필요한 경우 – 기본 accessor의 수정 없이 body없는 accessor를 통해 정의 가능 – Body를 작성해 주어도 됨 새차원 var setterVisibility: String = "abc" private set var setterWithAnnotation: Any? = null @Inject set // annotate the setter with Inject var setterVisibility: String = "abc" private set(value) { field = value }
  • 10. Properties and Fields • Backing Fields – 코틀린 클래스는 field를 가질 수 없음 – ‘field’라는 식별자를 통해 접근할 수 있는, automatic backing field를 제공함 – field는 프로퍼티의 accessor에서만 사용가능 함 새차원 // the initializer value is written directly // to the backing field var counter = 0 set(value) { if (value >= 0) field = value }
  • 11. Properties and Fields • Backing Fields 생성 조건 – accessor중 1개라도 기본 구현을 사용하는 경우 – custom accessor에서 field 식별자를 참조하는 경우 – 아래의 경우는 backing field를 생성하지 안음 새차원 // the initializer value is written directly to the backing field var counter = 0 set(value) { if (value >= 0) field = value } val isEmpty: Boolean get() = this.size == 0
  • 12. Properties and Fields • Backing Properties – “implicit backing field“ 방식이 맞지 안는 경우에는 – “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("null ") }
  • 13. Properties and Fields • Compile-Time Constants – const modifier를 이용하면 컴파일 타임 상수를 만들 수 있음 • 이런 프로퍼티는 어노테이이션 에서도 사용 가능함 – 조건 • Top-level 이거나 • object의 멤버이거나 • String나 프리미티브 타입으로 초기화된 경우 • custom getter이 없는 경우 새차원 const val SUBSYSTEM_DEPRECATED: String = "This subsystem is deprecated" @Deprecated(SUBSYSTEM_DEPRECATED) fun foo() { ... }
  • 14. Properties and Fields • Late-Initialized Properties – 일반적으로 프로퍼티는 non-null 타입으로 선언됨 – 간혹 non-null 타입 프로퍼티를 사용하고 싶지만, 생성자에서 초 기화를 해줄 수 없는 경우가 있음 • Dependency injection • Butter knife • Unit test의 setup 메소드 새차원 public class MyTest { lateinit var subject: TestSubject @SetUp fun setup() { subject = TestSubject() } @Test fun test() { subject.method() // dereference directly } }
  • 15. Properties and Fields • Late-Initialized Properties – 객체가 constructor에서는 할당 되지 못하지만 – 여전히 non-null타입으로 사용하고 싶은 경우 – Lateinit modifier를 사용가능 – 조건 • 클래스의 바디에서 선언된 프로퍼티만 가능 • 기본생성자에서 선언된 프로퍼티는 안 됨 • var 프로퍼티만 가능 • custom accessor이 없어야 함 • non-null 타입이어야 함 • 프리미티브 타입이면 안됨 – lateinit 프로퍼티가 초기화 되기 전에 접근되면, 오류가 발생됨 • kotlin.UninitializedPropertyAccessException: lateinit property tet has not been initialized 새차원