(Swift) 인스턴스 메서드 , 타입 메서드 (instance method, type method)
Swift Method Type
class SampleClass {
func instanceMethod() {}
static func staticMethod() {}
class func classMethod() {}
}
class
,static
키워드를 통해 정의된 함수를 Type Method
라고 한다.
struct FruitStore {
var count: Int = 0 {
didSet {
print("count updated \\(count)")
}
}
mutating func setupCount() {
self.count += 1
}
}
또 한가지 중요한 점은 타입 메서드 내부에서 self는 타입 그 자체를 가리킨다(인스턴스 메서드는 인스턴스를 가리킴)
class ParentClass {
class func callParentName() {
print("Super Class \\(self)")
}
static func callParentName2() {
print("Super Class \\(self)")
}
}
class ChildClass: ParentClass {
// >>> 오버라이드 가능
override class func callParentName(){
print("this is \\(self)")
}
// >>> 오버라이드 불가 컴파일 에러
override static func callParentName2(){
print("this is \\(self)")
}
}
Class Method
: **override
**가 가능하다.
Static Methdo
: **override
**가 불가능 하다.