[swift] 34. class func vs static func

RudinP·2023년 10월 3일
0

Study

목록 보기
52/227

static func

  • final class func와 동일한 방식
    • override 불가
  • 메모리에 올리지 않고(객체를 생성하지 않고) 사용 가능
class Friend{
	func sayHi(){
    	print("안녕")
    }
    final class func sayHo(){
    	print("호호")
    }
    static func sayHoo{
    	print("후후")
    }
}

class BestFriend: Friend{
	class override func sayHi(){
    	print("덮어씌운 안녕")
    }
    func sayHoo(){
    	print("덮어씌운 호호")
    }
}

let myFriend = Friend()
myFriend.sayHi() //안녕
Friend.sayHo() //호호
Friend.sayHoo() //후후

BestFriend.sayHi() //덮어씌운 안녕
BestFriend.sayHoo() //후후

사용 예 (절대적이지 않음)

  • Utils class 생성
    • helper 메소드 모음
    • converting 함수
profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글