在单独的类[Swift]中声明类变量

时间:2015-03-12 17:38:29

标签: ios swift swift-playground

这里我有一个类,Player,有一个类型变量Sport,其中可以是篮球或足球。我希望能够在玩家宣言中宣布体育类型。有什么建议吗?

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")

3 个答案:

答案 0 :(得分:1)

您也可以像这样使用枚举:

enum Sport {
    case Soccer (teamName : String)

    var players: Int {
        switch self{
            case .Soccer: return 11
            default: return 0
        }
    }
}

class Player {
    let sport: Sport?
    init? (s : Sport){
        self.sport = s
    }
}

Sport.Soccer (teamName: "Cambuur").players

答案 1 :(得分:0)

在这种情况下,我建议你的例子是这个解决方案,我不知道是否有更好的方法,但在我对OOP的研究中,我相信这是一个很好的方式来做你的榜样。

protocol Sport {
     func getTotalPlayers() -> Int
}

class Soccer: Sport {
      func getTotalPlayers() -> Int {
          return 11
      }
}

class Basketball: Sport {
     func getTotalPlayers() -> Int {
          return 5
     }
}

class Team {
    private var sport: Sport
    private var name: String

    init(sport:Sport, name:String) {
        self.sport = sport
        self.name = name
    }

    func getTeamName() -> String {
        return name
    }

    func getSport() -> Sport {
          return sport
    }
}

class Chelsea: Team {
    init() {
        super.init(sport: Soccer(), name: "Chelsea")
    }
}

class Wizards: Team {
    init() {
        super.init(sport: Basketball(), name: "Wizards")
    }
}

class Player {

    private var team: Team

    init(team: Team) {
        self.team = team
    }

    func getTeamName() -> String {
        return self.team.getTeamName()
    }  

    func getSport() -> Sport {
        return self.team.getSport()
    }
}

let me = Player(team: Chelsea())
let him = Player(team: Wizards())

答案 2 :(得分:0)

我找到了一种方法来做到这一点..如果你在播放器初始化函数Sport.Type中声明了typeOfSport,然后像这样使用Sport初始化方法....

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 } }

    required init(teamName: String) {
        self.teamName = teamName
    }

}

class Player {
    let sport : Sport?

    init? (typeOfSport: Sport.Type, teamName: String) {
        self.sport = Soccer(teamName: teamName)
    }

    init? (typeOfSport: Basketball, teamName: String) {
        self.sport = Basketball(teamName: teamName)
    }
}

let me = Player(typeOfSport: Soccer.self, teamName: "chelsea")

let him = Player(typeOfSport: Basketball.self, teamName: "wizards")