swift

swift - Protocols #7 (Protocol Composition)

행복하게사는게꿈 2021. 1. 12. 19:09

protocol Resettable {
   func reset()
}

protocol Printable {
   func printValue()
}

class Size: Resettable, Printable {
   var width = 0.0
   var height = 0.0
   
   func reset() {
      width = 0.0
      height = 0.0
   }
   
   func printValue() {
      print(width, height)
   }
}

class Circle: Resettable {
   var radius = 0.0
   
   func reset() {
      radius = 0.0
   }
}

class Oval: Circle {
   
}

let r: Resettable = Size()
let p: Printable = Size()

var rp: Resettable & Printable = Size()

var cr: Circle & Resettable = Circle()

cr = Oval()