๐ iOS/Swift
Class๋ฉ์๋์ Static๋ฉ์๋
JerryiOS
2023. 6. 8. 20:36
๋ฉ์๋
ํด๋์ค, ๊ตฌ์กฐ์ฒด, ์ด๊ฑฐํ ์์ ํฌํจ๋์ด ์๋ ํจ์
์ธ์คํด์ค ๋ฉ์๋(Instance Method)
์ธ์คํด์ค๋ฅผ ์์ฑํด์ผ๋ง ํธ์ถ์ด ๊ฐ๋ฅํ ๋ฉ์๋
class Jerry {
func doSomething() {
print("dd")
}
}
์ด๋ฐ ๋ฉ์๋๊ฐ ์ธ์คํด์ค ๋ฉ์๋๋ค.
let jerry: Jerry = .init()
jerry.doSomething()
์ธ์คํด์ค๋ฅผ ์์ฑํด์ผ๋ง ํธ์ถ ๊ฐ๋ฅํ๋ค. ๊ทธ๋์ ์ธ์คํด์ค ๋ฉ์๋๋ค.
ํ์ ๋ฉ์๋(Type Method)
์ธ์คํด์ค ์์ฑ์์ด ํ์(Type) ์ด๋ฆ๋ง ์๋ฉด ํธ์ถ์ด ๊ฐ๋ฅํ ๋ฉ์๋
class Jerry {
static func typeMethod1() {
print("dd")
}
class func typeMethod2() {
print("ww")
}
}
Static ๋ฉ์๋
SubClass์์ ํด๋น ํ์ ๋ฉ์๋์ ์ค๋ฒ๋ผ์ด๋ฉ์ ๊ธ์งํ๋ ๋ฉ์๋
class Jerry {
static func sayHello() {
print("Hello")
}
}
class ChildJerry: Jerry {
override static func sayHello() { //Cannot override static method
}
}
Class ๋ฉ์๋
SubClass์์ ํด๋น ํ์ ๋ฉ์๋์ ์ค๋ฒ๋ผ์ด๋ฉ์ ํ์ฉํ๋ ๋ฉ์๋
class Jerry {
class func sayBye() {
print("Bye")
}
}
class ChildJerry: Jerry {
override class func sayBye() {
}
}
๋ฐ์ํ