Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol Fighter{
- func fight() //Requirement method
- // func punch() //Requirement. Uncomment to see changes
- }
- extension Fighter{
- //Requirement common implementation
- func fight(){
- self.punch()
- }
- //Static method (it is not defined in the protocol)
- func punch(){
- print("Fighter punch")
- }
- }
- protocol MagicalFighter : Fighter{
- func castSpell() //Requirement
- }
- extension MagicalFighter{
- //Requirement common implementation
- func castSpell(){
- print("MagicalFighter casted a spell")
- }
- //Static
- func punch(){
- self.castSpell()
- print("Magical fighter punch")
- }
- // Requirement. Uncomment to see changes
- func fight(){
- self.punch()
- }
- }
- struct Hero : MagicalFighter{
- func castSpell(){
- print("Hero casted a special spell")
- }
- }
- let gordo = Hero()
- gordo.fight()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement