Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Swift 3
- //
- import UIKit
- /*
- 1) Properties (type & instance)
- 2) Initializers
- 3) Deinitializers
- 4) Methods (type & instance)
- 5) Subscripts
- */
- class Vehicule {
- // 1) Type Properties
- static var count = 0
- // 1) Instance Properties
- var passengersCapacity = 4
- let zeroTo60: Float
- var color: UIColor
- // 2) Initializers
- init(passengers: Int, zeroTo60: Float, color: UIColor = UIColor.black) {
- passengersCapacity = passengers
- self.zeroTo60 = zeroTo60
- self.color = color
- Vehicule.count += 1
- }
- // You have to use 'convenience' keyword for initializers who call another initializers
- convenience init(zeroTo60: Float) { self.init(passengers: 4, zeroTo60: zeroTo60) }
- convenience init() { self.init(zeroTo60: 6.0) }
- // 3) Deinitializers
- deinit {
- Vehicule.count +-= 1
- }
- // 4) Type Method (you can use 'static' or 'class' keyword)
- class func printCount() {
- print("Count:", count) // Without the classname prefix ('Vehicule.')
- }
- // 4) Instance Method
- func start() {
- print("(Silence)")
- }
- }
- let teslaModelS = Vehicule(passengers: 4, zeroTo60: 2.5)
- let teslaModel3 :Vehicule? = Vehicule()
- print(Vehicule.count) // = 2
- // Implicit use of Deinitializers
- teslaModel3 = nil
- print(Vehicule.count) // = 1
- // By reference
- let p100d = teslaModelS
- p100d.color = UIColor.red
- // teslaModelS.color == UIColor.red
- // Type Method
- Vehicule.printCount()
- // Instance Method
- teslaModelS.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement