Here I have a class, Player, that has a variable of type, Sport, of which can be Basketball or Soccer. I'd like to be able to declare the type of Sport in the Player declaration. Any suggestions?
class Soccer : Sport {
override var players : Int { get { return 11 } }
}
class Basketball : Sport {
override var players : Int { get { return 5 } }
}
class Sport {
var teamName: String
var players: Int { get { return 0 } }
init(teamName: String) {
self.teamName = teamName
}
}
class Player {
let sport : Sport?
init? (typeOfSport: Soccer, teamName: String) {
self.sport = Soccer(teamName: teamName)
}
init? (typeOfSport: Basketball, teamName: String) {
self.sport = Basketball(teamName: teamName)
}
}
let me = Player(typeOfSport: Soccer(), teamName: "chelsea")
let him = Player(typeOfSport: Basketball(), teamName: "wizards")