티스토리 뷰
출처
상속 (Inheritance)
상속 기본
class Vehicle {
var currentSpeed = 0.0
var description: String {
return "traveling at \(currentSpeed) miles per hour"
}
func makeNoise() {
// do nothing - an arbitrary vehicle doesn't necessarily make a noise
}
}
let someVehicle = Vehicle()
print("Vehicle: \(someVehicle.description)")
// Vehicle: traveling at 0.0 miles per hour
상속된 클래스에서 상위 클래스의 프로퍼티에 접근할 수 있다.
class Bicycle: Vehicle {
var hasBasket = false
}
let bicycle = Bicycle()
bicycle.hasBasket = true
bicycle.currentSpeed = 15.0
print("Bicycle: \(bicycle.description)")
class Tandem: Bicycle {
var currentNumberOfPassengers = 0
}
let tandem = Tandem()
tandem.hasBasket = true
tandem.currentNumberOfPassengers = 2
tandem.currentSpeed = 22.0
print("Tandem: \(tandem.description)")
// Tandem: traveling at 22.0 miles per hour
오버라이딩 (Override)
class Train: Vehicle {
override func makeNoise() {
print("Choo Choo")
}
}
class Car: Vehicle {
var gear = 1
override var description: String {
return super.description + " in gear \(gear)"
}
}
let car = Car()
car.currentSpeed = 25.0
car.gear = 3
print("Car: \(car.description)")
// Car: traveling at 25.0 miles per hour in gear 3
프로퍼티 옵저버 오버라이딩
class AutomaticCar: Car {
override var currentSpeed: Double {
didSet {
gear = Int(currentSpeed / 10.0) + 1
}
}
}
let automatic = AutomaticCar()
automatic.currentSpeed = 35.0
print("AutomaticCar: \(automatic.description)")
// AutomaticCar: traveling at 35.0 miles per hour in gear 4
오버라이드 막기 (Preventing Overrides)
final var someValue : Int
'iOS 개발 > Swift' 카테고리의 다른 글
[Swift 3] 해제 (Deinitialization) (0) | 2017.04.15 |
---|---|
[Swift 3] 초기화 (Initialzer) (0) | 2017.04.15 |
[Swift 3] 서브스크립트 (Subscript) (0) | 2017.04.15 |
[Swift 3] 메소드 (Method) (0) | 2017.04.15 |
[Swift 3] 프로퍼티 (Property) (0) | 2017.04.15 |
- Total
- Today
- Yesterday
- docker
- Swift 3
- optional
- Swift3
- CGImage
- HTTP
- ios
- 읽기 좋은 코드가 좋은 코드다
- string
- coredata
- Swift 3.0
- RunLoop
- dictionary
- EffectiveObjectiveC
- Arc
- CIImage
- NSManagedObjectModel
- Block
- thread
- NSManagedObject
- Swfit
- applicationWillResignActive
- delegate
- UIView
- NSManagedObjectContext
- 꺼내먹어요
- set
- AWS
- workerThread
- Swift
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |