Using a generic class for multiple types

We can create instances of the Party<AnimalElement> class by replacing the AnimalElement generic type parameter with any type name that conforms to the constraints specified in the declaration of the Party<AnimalElement> class. So far, we have three concrete classes that implement both the AnimalProtocol and Equatable protocols: Dog, Frog, and Lion. Thus, we can use Dog to create an instance of Party<Dog>--that is, a Party instance of Dog objects. The following code shows the lines that create four instances of the Dog class: jake, duke, lady, and dakota. Then, the code creates a Party<Dog> instance named dogsParty and passes jake as the leader argument to the initializer. This way, we will create a party ...

Get Swift 3 ObjectOriented Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.