Skip to content

Instantly share code, notes, and snippets.

@abhimuralidharan
Created May 29, 2017 09:25
Show Gist options
  • Select an option

  • Save abhimuralidharan/c2fe436dbf41da7c83c21984e9c81565 to your computer and use it in GitHub Desktop.

Select an option

Save abhimuralidharan/c2fe436dbf41da7c83c21984e9c81565 to your computer and use it in GitHub Desktop.
class Player {
static var totalNumberOfPlayers = 0
var name:String
init(name:String) {
Player.totalNumberOfPlayers += 1
self.name = name
}
static func printTotalNumberOfPlayers() {
print("Function log --> Total no of players: \(Player.totalNumberOfPlayers)")
}
class func printGameName() {
print("Road runner")
}
}
let a = Player(name: "abhi")
let b = Player(name: "joe")
print("Total no of players: \(Player.totalNumberOfPlayers)")// prints Total no of players: 2
Player.printTotalNumberOfPlayers() // print Function log --> Total no of players: 2
class Enemy: Player {
override class func printGameName() {
super.printGameName()
print("Super mario")
}
// override static func printTotalNumberOfPlayers() { //error: cannot override static method
// print("Function log --> Total no of players: \(Player.totalNumberOfPlayers)")
// }
}
Enemy.printGameName() prints Road runner and Super mario as super method is also called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment