Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.91 KB | None | 0 0
  1. class NamedShape {
  2.     var numberOfSides: Int = 0
  3.     var name: String
  4.    
  5.     init(name: String) {
  6.         self.name = name
  7.     }
  8.    
  9.     func simpleDescription() -> String {
  10.         return "A shape with \(numberOfSides) sides."
  11.     }
  12. }
  13.  
  14. class Circle: NamedShape {
  15.     var radius: Double
  16.    
  17.     init(radius: Double, name: String) {
  18.         self.radius = radius
  19.         super.init(name: name)
  20.     }
  21.    
  22.     var area: Double {
  23.         get {
  24.             return Double.pi * pow(radius, 2)
  25.         }
  26.         set {
  27.             radius = sqrt(area / Double.pi)
  28.         }
  29.     }
  30.    
  31.     override func simpleDescription() -> String {
  32.         return "A circle with radius \(radius)"
  33.     }
  34. }
  35.  
  36. let test = Circle(radius: Double(10), name: "my test circle")
  37.  
  38. test.area
  39. test.simpleDescription()
  40. test.area = 90
  41. test.radius // shouldn't this set radius to a new value? the playground is still returning 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement