Advertisement
Guest User

Untitled

a guest
May 16th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.94 KB | None | 0 0
  1. protocol Fighter{
  2.     func fight() //Requirement method
  3. //    func punch() //Requirement. Uncomment to see changes
  4. }
  5.  
  6. extension Fighter{
  7.     //Requirement common implementation
  8.     func fight(){
  9.         self.punch()
  10.     }
  11.    
  12.     //Static method (it is not defined in the protocol)
  13.     func punch(){
  14.         print("Fighter punch")
  15.     }
  16. }
  17.  
  18. protocol MagicalFighter : Fighter{
  19.     func castSpell() //Requirement
  20. }
  21.  
  22. extension MagicalFighter{
  23.     //Requirement common implementation
  24.     func castSpell(){
  25.         print("MagicalFighter casted a spell")
  26.     }
  27.    
  28.     //Static
  29.     func punch(){
  30.         self.castSpell()
  31.         print("Magical fighter punch")
  32.     }
  33.    
  34. //    Requirement. Uncomment to see changes
  35.         func fight(){
  36.             self.punch()
  37.         }
  38.    
  39. }
  40.  
  41. struct Hero : MagicalFighter{
  42.     func castSpell(){
  43.         print("Hero casted a special spell")
  44.     }
  45. }
  46.  
  47. let gordo = Hero()
  48. gordo.fight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement