Last active
August 9, 2017 11:43
-
-
Save qnoid/1499fb8e0e344f706e00 to your computer and use it in GitHub Desktop.
Swift ensures that all properties of a derived class are initialised before calling its base class. The compiler will generate a warning. Just move the statement below the assignment to remove the warning. Compared to Java, this is safe. See: http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class Plane | |
{ | |
init() | |
{ | |
readyToTakeOff(); | |
} | |
func readyToTakeOff(){ | |
} | |
} | |
class HighSpeedPlane : Plane | |
{ | |
var speed : Int | |
init(speed: Int) { | |
super.init() //compiler warning: "Property 'self.speed' not initialized at super.init call" | |
self.speed = speed | |
} | |
override func readyToTakeOff() | |
{ | |
super.readyToTakeOff() | |
println("Travelling at the speed of \(self.speed)") | |
} | |
} | |
let plane = HighSpeedPlane(speed: 200); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment