Skip to content

[Swift] 타입 속성 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seungchan2 opened this issue May 17, 2022 · 0 comments
Closed

[Swift] 타입 속성 #44

seungchan2 opened this issue May 17, 2022 · 0 comments
Assignees
Labels

Comments

@seungchan2
Copy link
Owner

타입 속성

  • 인스턴스에 속한 속성이 아니고, 타입자체에 속한 속성이기에 내/외부에서 Type.property로 접근해야함

  • 저장 타입 속성을 주로 사용

저장 타입 속성

모든 인스턴스가 동일하게 가져야하는 보편적인 속성이거나

모든 인스턴스가 공유해야하는 성격에 가까운 저장속성을 저장 타입 속성으로 선언

  • static 키워드 사용. (상속시)재정의 불가(메서드만 상속이 가능 - class 키워드 사용불가)

  • let var 선언 둘다 가능(저장 타입 속성)

  • 항상 기본값(초기값) 필요(생성자에 의한 값 설정 과정이 없으므로)

  • 자체적으로 지연(lazy) 속성의 성격을 가지므로, 호출시 메모리 할당(내부적으로 Thread-Safe 처리)

class Circle {
    static let pi = 3.141592
    static var count= 0
}

계산 타입 속성

  • (상속시 )재정의 가능(class 키워드 사용 시에만)

  • static(의미: 고정적인) 또는 class 키워드 사용(static 상속시 재정의 불가/class 상속시 재정의 가능 의미임)

  • var 키워드만 사용 가능(계산 타입 속성)

  • 메서드이기 때문에 타입에 메모리 공간이 할당되어 있지 않음(계산 속성)

class Circle {
    static let pi = 3.141592
    static var multiPi: Double {
          return pi * 2
    }
}
@seungchan2 seungchan2 self-assigned this May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant