daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest May 16th, 2018 131 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top