简体   繁体   English

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

[英]Declare class type of variable in seperate class [Swift]

Here I have a class, Player, that has a variable of type, Sport, of which can be Basketball or Soccer. 在这里,我有一个Player类,该类的变量类型为Sport,可以是Basketball或Soccer。 I'd like to be able to declare the type of Sport in the Player declaration. 我希望能够在Player声明中声明Sport的类型。 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")

You could also use an enum for this like this: 您也可以像这样使用枚举:

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

In this case I suggest for your example is this solution, I don't know if is the better way, but in my studies about OOP, I believe it is a cool way to do your example. 在这种情况下,我建议您使用的示例就是这种解决方案,我不知道这是否是更好的方法,但是在我对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())

I found a way to do this.. If you declare the typeOfSport in the player initialization function Sport.Type and then make the Sport initialization method required like so.... 我找到了一种方法。如果您在播放器初始化函数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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM