O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using a generic class with two generic type parameters

We can create instances of the PartyWithDeeJay<T, K> class by replacing both the T and K generic type parameters with any type names that conform to the constraints specified in the declaration of the PartyWithDeeJay<T, K> class. We have three concrete classes that implement both the AnimalProtocol and Equatable protocols: Dog, Frog, and Lion. We have one class that conforms to the DeeJayProtocol protocol: HorseDeeJay. Thus, we can use Dog and HorseDeeJay to create an instance of PartyWithDeeJay<Dog, HorseDeeJay>.

The following lines create a HorseDeeJay instance named silver. Then, the code creates a PartyWithDeeJay<Dog, HorseDeeJay> instance named silverParty and passes jake and silver as ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required