Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. //
  2. // Swift 3
  3. //
  4.  
  5. import UIKit
  6.  
  7. /*
  8. 1) Properties (type & instance)
  9. 2) Initializers
  10. 3) Deinitializers
  11. 4) Methods (type & instance)
  12. 5) Subscripts
  13. */
  14. class Vehicule {
  15.  
  16. // 1) Type Properties
  17. static var count = 0
  18.  
  19. // 1) Instance Properties
  20. var passengersCapacity = 4
  21. let zeroTo60: Float
  22. var color: UIColor
  23.  
  24. // 2) Initializers
  25. init(passengers: Int, zeroTo60: Float, color: UIColor = UIColor.black) {
  26. passengersCapacity = passengers
  27. self.zeroTo60 = zeroTo60
  28. self.color = color
  29. Vehicule.count += 1
  30. }
  31.  
  32. // You have to use 'convenience' keyword for initializers who call another initializers
  33. convenience init(zeroTo60: Float) { self.init(passengers: 4, zeroTo60: zeroTo60) }
  34.  
  35. convenience init() { self.init(zeroTo60: 6.0) }
  36.  
  37. // 3) Deinitializers
  38. deinit {
  39. Vehicule.count +-= 1
  40. }
  41.  
  42. // 4) Type Method (you can use 'static' or 'class' keyword)
  43. class func printCount() {
  44. print("Count:", count) // Without the classname prefix ('Vehicule.')
  45. }
  46.  
  47. // 4) Instance Method
  48. func start() {
  49. print("(Silence)")
  50. }
  51. }
  52.  
  53. let teslaModelS = Vehicule(passengers: 4, zeroTo60: 2.5)
  54. let teslaModel3 :Vehicule? = Vehicule()
  55. print(Vehicule.count) // = 2
  56.  
  57. // Implicit use of Deinitializers
  58. teslaModel3 = nil
  59. print(Vehicule.count) // = 1
  60.  
  61. // By reference
  62. let p100d = teslaModelS
  63. p100d.color = UIColor.red
  64. // teslaModelS.color == UIColor.red
  65.  
  66. // Type Method
  67. Vehicule.printCount()
  68.  
  69. // Instance Method
  70. teslaModelS.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement