본문 바로가기
Swift

[Swift] 11. 클래스

by CodingKwon 2021. 10. 6.

1. 클래스

클래스는 참조 타입

타입 이름은 대문자 카멜케이스를 사용

class 이름 {
	/* 구현부 */
}
  • 클래스의 타입 메서드는 두 가지 종류가 있음
    • 1. 상속 후 재정의가 가능한 class 타입 매서드
    • 2. 상속 후 재정의가 불가능한 static 타입 매서드
class Sample {
    // 가변 프로퍼티
    var mutableProperty: Int = 100 

    // 불변 프로퍼티
    let immutableProperty: Int = 100 
    
    // 타입 프로퍼티
    static var typeProperty: Int = 100 
    
    // 인스턴스 메서드
    func instanceMethod() {
        print("instance method")
    }
    
    // 타입 메서드
    // 재정의 불가 타입 메서드 - static
    static func typeMethod() {
        print("type method - static")
    }
    
    // 재정의 가능 타입 메서드 - class
    class func classMethod() {
        print("type method - class")
    }
}

 

'Swift' 카테고리의 다른 글

[Swift] 13. 값 타입, 참조 타입  (0) 2021.10.07
[Swift] 12. 열거형  (0) 2021.10.06
[Swift] 10. 구조체  (0) 2021.10.06
[Swift] 9. optional 옵셔널  (0) 2021.10.06
[Swift] 8. 반복문  (0) 2021.10.05

댓글