Created
February 15, 2023 16:50
-
-
Save foxicode/1b4d7bc8f8ee9f73957e6167cc9f7254 to your computer and use it in GitHub Desktop.
Protocols with static functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| protocol Hero { | |
| static func weapon() -> String | |
| static func fight() | |
| } | |
| extension Hero { | |
| static func fight() { | |
| print("Fighting with... \(weapon())") | |
| } | |
| } | |
| class Archer: Hero { | |
| static func weapon() -> String { | |
| "bow with arrows" | |
| } | |
| } | |
| class Knight: Hero { | |
| static func weapon() -> String { | |
| "sword" | |
| } | |
| } | |
| func fightFunc(character: Hero.Type) { | |
| character.fight() | |
| } | |
| let myCharacter = Archer() | |
| type(of: myCharacter).self.fight() | |
| fightFunc(character: type(of: myCharacter).self) | |
| Archer.fight() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment