Inheritance
A class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass.
μ€μννΈ μμ
- ν΄λμ€, νλ‘ν μ½μ μμμ΄ κ°λ₯νμ§λ§ μ΄κ±°ν, ꡬ쑰체λ μμμ΄ λΆκ°λ₯
- μ€μννΈμ ν΄λμ€λ λ¨μΌμμμΌλ‘, λ€μ€μμμ μ§μνμ§ μμ΅λλ€.
- final ν€μλλ₯Ό μ¬μ©νλ©΄ μ¬μ μ(override)λ₯Ό λ°©μ§ν μ μμ΅λλ€.
- static ν€μλλ₯Ό μ¬μ©ν΄ νμ λ©μλλ₯Ό λ§λ€λ©΄ μ¬μ μκ° λΆκ°λ₯ ν©λλ€.
- class ν€μλλ₯Ό μ¬μ©ν΄ νμ λ©μλλ₯Ό λ§λ€λ©΄ μ¬μ μκ° κ°λ₯ν©λλ€.
- class μμ finalμ λΆμ΄λ©΄ static ν€μλλ₯Ό μ¬μ©νκ²κ³Ό λμΌνκ² λμν©λλ€.
- override ν€μλλ₯Ό μ¬μ©ν΄ λΆλͺ¨ ν΄λμ€μ λ©μλλ₯Ό μ¬μ μ ν μ μμ΅λλ€.
μμꡬν
class Person {
var name: String = ""
func selfIntroduce() {
print("μ λ \(name)μ
λλ€")
}
// final : μ¬μ μ λΆκ° λ©μλ
final func sayHello() {
print("hello")
}
// static : μ¬μ μ λΆκ° νμ
λ©μλ
static func typeMethod() {
print("type method - static")
}
// class : μ¬μ μ κ°λ₯ νμ
λ©μλ
class func classMethod() {
print("type method - class")
}
// `final class`λ staticκ³Ό λκ°μ μν : μ¬μ μ λΆκ°
final class func finalCalssMethod() {
print("type method - final class")
}
}
// Personμ μμλ°λ Student
class Student: Person {
var major: String = ""
override func selfIntroduce() {
print("μ λ \(name)μ΄κ³ , μ 곡μ \(major)μ
λλ€")
}
override class func classMethod() {
print("overriden type method - class")
}
// μ¬μ μ λΆκ°
// override static func typeMethod() { }
// override func sayHello() { }
// override class func finalClassMethod() { }
}
'iOS π > Swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Swift14 - Optional Chaining (0) | 2021.02.03 |
---|---|
Swift13 - Initialization & Deinitialization (0) | 2021.02.02 |
Swift11 - νλ‘νΌν° (0) | 2021.02.02 |
Swift10 - first class citizen (0) | 2021.02.02 |
Swift09 - ν΄λ‘μ (0) | 2021.02.02 |