티스토리 뷰
object VS class
https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview
class
- 인스턴스를 변수에 대입하여 사용하는 방식이다.
object
- expressions: 한번 사용하기 좋으며 익명 클래스라고도 한다.
- declarations: 싱글톤 패턴에 유용하게 사용가능하다.
Object expressions
Object expressions create objects of anonymous classes, that is, classes that aren't explicitly declared with the class declaration. Such classes are useful for one-time use. You can define them from scratch, inherit from existing classes, or implement interfaces. Instances of anonymous classes are also called anonymous objects because they are defined by an expression, not a name.
Creating anonymous objects from scratch
Object expressions start with the object keyword.
If you just need an object that doesn't have any nontrivial supertypes, write its members in curly braces after object:
val helloWorld = object {
val hello = "Hello"
val world = "World"
// object expressions extend Any, so override is required on toString()
override fun toString() = "$hello $world"
}
Object declarations
The Singleton pattern can be useful in several cases, and Kotlin makes it easy to declare singletons:
This is called an object declaration, and it always has a name following the object keyword. Just like a variable declaration, an object declaration is not an expression, and it cannot be used on the right-hand side of an assignment statement.
The initialization of an object declaration is thread-safe and done on first access.
객체 표현식과 선언 간의 의미적 차이
객체 표현식과 객체 선언 사이에는 한 가지 중요한 의미론적 차이가 있습니다.
- 객체 표현식(Object expressions)은 사용되는 즉시 실행(및 초기화)됩니다 .
- 객체 선언(Object declarations)은 처음 액세스할 때 느리게 초기화됩니다 .
- 동반 객체(A companion object)는 Java 정적 초기화 프로그램의 의미 체계와 일치하는 해당 클래스가 로드(해결)될 때 초기화됩니다.
Reference
페이지에 한글과 영어 버전을 부분적으로 가져왔는데, 솔직히 expressions 와 declartaions 차이를 잘 모르겠다.
'IT > Android' 카테고리의 다른 글
gradle task minifyDebugWithR8 not found (0) | 2023.07.25 |
---|---|
TODO:: applicationcontext kotlin (0) | 2023.07.07 |
[Android] DI (1) | DI, Hilt, Dagger (0) | 2023.04.03 |
(TODO) [Kotlin] Flow Collector (0) | 2023.03.28 |
맥북에서 안드로이드 태블릿에 apk 설치하기 | Android File Transfer (0) | 2023.03.08 |
- Total
- Today
- Yesterday
- InShot
- 수학 #다이나믹프로그래밍 #게임이론
- AdobeClip
- 알고리즘
- VLLO
- 쇼코의미소
- 통신패턴
- Vlogr
- 청년내일채움공제
- 내채공
- EH
- android
- 동기메시징기술
- TKD
- 코딩테스트
- https://kotlinlang.org/docs/object-declarations.html
- Kotlin
- 클라우드 네이티브 애플리케이션 디자인 패턴
- 레포지토리패턴
- 안드로이드
- 아이폰영상편집어플후기
- Repository pattern
- 프로그래머스
- 디자인패턴
- 내채공만기
- 맥북개발환경
- gitlab_ssh_permission_denied
- 코틀린
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |