GregLeck

Classes

Oct 2nd, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.11 KB | None | 0 0
  1. // CLASSES
  2. // Like structs but with 5 important differences:
  3. // 1) Classes don't come with a memberwise initializer.
  4. // 2) Classes can inherit from other classes.
  5. // 3) A copy of a class points to the same thing as the original.
  6. //      Changing one changes the other.
  7. // 4) They have deinitializers (deinit).
  8. // 5) Mutability: No need to add "mutating" to methods that change vars.
  9.  
  10. // Create your own init
  11. class Dog {
  12.     var name: String
  13.     var breed: String
  14.    
  15.     init(name: String, breed: String) {
  16.         self.name = name
  17.         self.breed = breed
  18.     }
  19. }
  20.  
  21. let chico = Dog(name: "Chico", breed: "Boxer Mix")
  22.  
  23. // INHERITANCE
  24. // Also known as subclassing
  25. // Class you inherit from is the "parent" or "super" class
  26. // New class is the "child" class
  27. // super.init is always called from child's init()
  28. class Poodle: Dog {
  29.     init(name: String) {
  30.         super.init(name: name, breed: "Poodle")
  31.     }
  32. }
  33.  
  34. // Another example of inheritance
  35. class Handbag {
  36.     var price: Int
  37.     init(price: Int) {
  38.         self.price = price
  39.     }
  40. }
  41. class DesignerHandbag: Handbag {
  42.     var brand: String
  43.     init(brand: String, price: Int) {
  44.         self.brand = brand
  45.         super.init(price: price)
  46.     }
  47. }
  48.  
  49. let gucci = DesignerHandbag(brand: "Gucci", price: 5)
  50.  
  51. // OVERRIDING METHODS
  52. class Superhero {
  53.     func superpower() {
  54.         print ("Super strength")
  55.     }
  56. }
  57.  
  58. class Batman: Superhero {
  59.     override func superpower() {
  60.         print("I'm rich")
  61.     }
  62. }
  63.  
  64. let superman = Superhero()
  65. superman.superpower()           // "Super strength"
  66. let batman = Batman()
  67. batman.superpower()             // "I'm rich"
  68.  
  69. // FINAL CLASSES
  70. // Keyword "final" prevents other classes from inheriting from it
  71. // and therefore incapable of overriding your methods
  72. final class Spaceship {
  73.     var name: String
  74.    
  75.     init(name: String) {
  76.         self.name = name
  77.     }
  78.    
  79.     func message() {
  80.         print("The name of the ship is \(name)")
  81.     }
  82. }
  83.  
  84. // class Enterprise: Spaceship {} <--- Returns error message
  85.  
  86. // COPYING CLASSES
  87. class Singer {
  88.     var name: String
  89.    
  90.     init(name: String) {
  91.         self.name = name
  92.     }
  93. }
  94.  
  95. var taylor = Singer(name: "Taylor")
  96. print(taylor.name)          // "Taylor"
  97.  
  98. var justin = taylor
  99. justin.name = "Justin"
  100. print(justin.name)          // "Justin"
  101. print(taylor.name)          // "Justin"
  102.  
  103. // DEINITIALIZERS
  104. class EmptyClass {
  105.    
  106. }
  107.  
  108. class Soldier {
  109.    
  110.     var name = "Greg"
  111.    
  112.     init() {
  113.         print("Hey, my name is \(name) and I'm alive and fighting!")
  114.     }
  115.    
  116.     func message() {
  117.         print("Just wanted to say hello!")
  118.     }
  119.    
  120.     deinit {
  121.         print("Now I'm dead... thanks a lot, deinit!")
  122.     }
  123. }
  124.  
  125. for _ in 1...2 {
  126.     let greg = Soldier()
  127.     greg.message()
  128. }
  129.  
  130. // MUTABILITY
  131.  
  132. class Artist {
  133.    
  134.     var name: String
  135.    
  136.     init(name: String) {
  137.         self.name = name
  138.     }
  139. }
  140.  
  141. let joanne = Artist(name: "Joanne")
  142. print(joanne.name)                  // "Joanne"
  143. joanne.name = "Emma"
  144. print(joanne.name)                  // "Emma"
  145. // Doing the above with a struct would not be possible
Advertisement
Add Comment
Please, Sign In to add comment